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.
A (regular) button click event is a routed event.
So long as it isn't marked as handled, the event will propogate up the visual tree and any subscribed handler will pick it up.
Your description is a bit confusing, because you mention an unspecified UserControl, Usercontrol 1 and 2. but I gather the user clicks in UC2 and that's "inside" UC1 and you want to handle the click event in UC1.
Do that by explicitly making e.handled = false; in any handler you subscribe in UC2.
The event should then "bubble up" and you can also handle it at UC1 level. NOTE this is not at some button in UC1, it's at UC1 level.
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/routed-events-overview
So you would wire up your handler in the UC1 tag, rather than a button in uc1.
Bear in mind you can template a button as any piece of UI you can define. So a Button can be templated as an image, usercontrol or whatever. Thus giving that whatever the behaviour of a button and click/command.
I do not follow why you can't just bind a command though. You could put the command in UC2's datacontext and use relativesource binding from UC1.