Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF RoutedEvent (via EventManager.RegisterClassHandler)

I'm trying to find a generic way to track "Modified" of any control (databind or not) in the current window (this is e.g. to enable or disable the Apply button in a typical Options window.) In fact, I just need to hook up a few event handlers like ComboBox.SelectionChanged or TextBox.TextChanged. In WinForms I had to loop through all controls in the form and hook up those events one by one, and it worked. In WPF, I thought I could get it done with much less code like

EventManager.RegisterClassHandler( typeof(MyCtrl)
                                 , ComboBox.SelectionChangedEvent
                                 , new RoutedEventHandler(OnChanged));

With this one line code, I would be able to wire up comobobox's selection changed event for all comboboxes in MyCtrl. However, the problem I ran into is that, if I see it correctly, this event is also fired when there is another control in the window that is not a combobox but also derived from "Selector". For example, I also have a datagrid on the same window (actually there is no combobox column in the datagrid), this event is also fired when I select a different row.

So, it appears that the above line of code hooks up the SelectionChanged event for all controls derived from Selector base, not just combobox.

Is this right? How can I just hook up the SelectionChanged event for all comboboxes?

EDIT with more examples: On the same control, I have a comboxbox and a button and I want to track Button.Click and ComboBox.SelectionChanged event as follows

EventManager.RegisterClassHandler( typeof(MyCtrl)
                                 , ComboBox.SelectionChangedEvent
                                 , new RoutedEventHandler(OnChanged));
EventManager.RegisterClassHandler( typeof(MyCtrl)
                                 , Button.ClickEvent
                                 , new RoutedEventHandler(OnChanged));

The problem is that I get Button.Click event right after I clicked the ComboBox dropdown button. How can I prevent this?

Here is some extra info for this event:

?e.OriginalSource
{System.Windows.Controls.Primitives.ToggleButton Content: 
            IsChecked:True}
    base {System.Windows.Controls.Primitives.ButtonBase}: 
            {System.Windows.Controls.Primitives.ToggleButton Content: IsChecked:True}
    IsChecked: true
    IsThreeState: false
?e.RoutedEvent
{ButtonBase.Click}
    HandlerType: {Name = "RoutedEventHandler" 
                  FullName = "System.Windows.RoutedEventHandler"}
    Name: "Click"
    OwnerType: {Name = "ButtonBase" 
                FullName = "System.Windows.Controls.Primitives.ButtonBase"}
    RoutingStrategy: Bubble
like image 641
newman Avatar asked Nov 05 '22 06:11

newman


1 Answers

I don't think that you can prevent this because those controls reuse the same events, which is normally a good thing. You could try to filter based on the sender and ignore the calls that come from controls that you are not interested in.

like image 98
Patrick Klug Avatar answered Nov 10 '22 20:11

Patrick Klug