I got a UserControl with a Click event that I want to be triggered by another Button's click event inside another UserControl. It doesn't work. UserControl2 is inside of UserControl1. I've been having this problem for a while, was using a RelayCommand class earlier with just MVVM in the rest of the app until the point when I had a scenario when I wanted to swap pictures on a button click. But the button and where the imageswapping happens shall be separated into two different UserControls. Anyway, I got the following XAML on Usercontrol 2 where the imageswapping is:
<Button x:Name="button" VerticalAlignment="Bottom" Height="48" Click="ClickData" />
Code behind UserControl2:
private bool switchimgbool = true;
public void ClickData(object sender, RoutedEventArgs e)
{
if (switchimgbool == true)
{ switchimgbool = false; }
else if (switchimgbool == false)
{ switchimgbool = true; }
switch (switchimgbool)
{
case false:
{
//some code
}
case true:
{
//some code
}
}
}
In UserControl1 xaml:
<Viewbox Stretch="Fill" Height="270" VerticalAlignment="Top">
<local:UserControl2></local:UserControl2>
</Viewbox>
///
<Button x:Name="executebutton" Click="executebutton_Click" />
Code behind UserControl1:
public static RoutedEvent SetClickData = EventManager.RegisterRoutedEvent("ClickData", RoutingStrategy.Bubble, typeof(RoutedEventhandler), typeof(UserControl2));
public event RoutedEventHandler ClickData
{
add { AddHandler(SetClickData, value);}
remove { RemoveHandler(SetClickData, value);}
}
UserControl2 us2 = new UserControl2();
private void executebutton_Click(object sender, RoutedEventArgs e)
{
us2.RaiseClickedEvent(sender);
}
The App is built with a lot of UserControls and buttons with different views. Like I said I used MVVM earlier with ViewModels and Views. However, this is the code in its current state but I've been trying around with different examples on the internet. But I don't get it to work, even tried doing a custom ClickRoutedEventArgs. Can only fire the Click event directly in UserControl2. I might not think clearly anymore the problem is just dragging me down. If anyone out there can provide me with some code or help me in the right direction I would be happy. Is there a fast way/fix I would be happy to do it.