I have a user control with a UniformGrid that has a number of Buttons. I want to assign the same handler to each Button's Click event. So, I added the following to the User Control:
public RoutedEventHandler GridButtonClickHandler
{
get { return ( RoutedEventHandler ) GetValue ( GridButtonClickHandlerProperty ); }
set { SetValue ( GridButtonClickHandlerProperty, value ); }
}
public static readonly DependencyProperty GridButtonClickHandlerProperty =
DependencyProperty.Register ( "GridButtonClickHandler", typeof ( RoutedEventHandler ), typeof ( UniformGrid ),
new PropertyMetadata ( GridButtonClickPropertyChanged ) );
private static void GridButtonClickPropertyChanged ( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
( ( UniformGrid ) o ).Children.OfType<Button> ( ).ToList ( ).ForEach ( b => b.Click += ( RoutedEventHandler ) e.NewValue );
}
Then, somewhere where there is a reference to the User Control (numpad in this example), I have this:
numpad.GridButtonClickHandler += btn_clicked;
I have breakpoints at the GridButtonClickHandler set and the GridButtonClickPropertyChanged method; the first hits when the assignment happens, but the second never hits.
See what I am doing wrong?