0

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.

CoyBit
  • 1,592
  • 1
  • 17
  • 19
Greg
  • 3
  • 4
  • It's a pity you've thrown away the MVVM approach and got back to the WinForms-ish way with events. The correctly implemented MVVM pattern will solve your problems very easily. Is there a reason for that change? – dymanoid Mar 15 '19 at 10:39
  • Well the reason behind it is because it's a panorama picture view and im too new to C# to be able to make that part work with MVVM. – Greg Mar 15 '19 at 11:09
  • please elaborate: "because it's a panorama picture view" – Denis Schaf Mar 15 '19 at 11:30

1 Answers1

0

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.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • Something like in US2: cmdClickData ? or i just have it public there as ICommand, tested alot of things – Greg Mar 15 '19 at 13:07
  • UC1: Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.cmdClickData}" – Greg Mar 15 '19 at 13:21
  • Ancestortype should be the specific usercontrol type such as local:UserControl1 or 2. If you just use usercontrol, it'll find the inner usercontrol. – Andy Mar 15 '19 at 14:48
  • ait basically in this case I want to push the button in UserControl 1 and execute the event in UserControl 2. so x:Type UserControl2 – Greg Mar 15 '19 at 15:18