Hi all I have the following xaml <UniformGrid MouseDown="UniformGrid_MouseDown"> <Button Name="btn0">Hello</Button> <Button Name="btn1"> <Button.Template> <ControlTemplate> <Rectangle Fill="Red" Stroke="Yellow" Opacity="0.7" StrokeThickness="5"></Rectangle> </ControlTemplate> </Button.Template> </Button> <Button Name="btn2"> <Button.Template> <ControlTemplate> <Ellipse Fill="Green" Stroke="Violet" Opacity="0.7" StrokeThickness="5"></Ellipse> </ControlTemplate> </Button.Template> </Button> </UniformGrid> and the following code for the code behind private void UniformGrid_MouseDown(object sender, MouseButtonEventArgs e) { FrameworkElement elm = e.OriginalSource as FrameworkElement; MessageBox.Show("You clicked " + elm.Name); } Why is it that the events are never raised? I want to catch all button mousedown events (ideally I would like to catch the click event for buttons) in a global handler...how do i do this?
On Jul 3, 11:41 am, Ilyas wrote: > Hi all > > I have the following xaml > > <UniformGrid MouseDown="UniformGrid_MouseDown"> > <Button Name="btn0">Hello</Button> > <Button Name="btn1"> > <Button.Template> > <ControlTemplate> > <Rectangle Fill="Red" Stroke="Yellow" > Opacity="0.7" StrokeThickness="5"></Rectangle> > </ControlTemplate> > </Button.Template> > </Button> > <Button Name="btn2"> > <Button.Template> > <ControlTemplate> > <Ellipse Fill="Green" Stroke="Violet" > Opacity="0.7" StrokeThickness="5"></Ellipse> > </ControlTemplate> > </Button.Template> > </Button> > </UniformGrid> > and the following code for the code behind > > private void UniformGrid_MouseDown(object sender, > MouseButtonEventArgs e) > { > FrameworkElement elm = e.OriginalSource as > FrameworkElement; > MessageBox.Show("You clicked " elm.Name); > } > > Why is it that the events are never raised? I want to catch all button > mousedown events (ideally I would like to catch the click event for > buttons) in a global handler...how do i do this? This is because MouseDown is a "bubbling" event - it means that child control on which the event happens handles it first, and only if it declines to handle it, the event is passed on to the parent of that control (and so on, until it reaches top). If you want to capture the event in a parent before the child sees it, you need to use a "tunneling" event - in your case, it would be PreviewMouseDown.
"Ilyas" wrote in message news:3c848a1b-a981-4f44-9e68-98d6452398a0@34g2000hsf.googlegroups.com... > Hi all > > I have the following xaml > > <UniformGrid MouseDown="UniformGrid_MouseDown"> > <Button Name="btn0">Hello</Button> > <Button Name="btn1"> > <Button.Template> > <ControlTemplate> > <Rectangle Fill="Red" Stroke="Yellow" > Opacity="0.7" StrokeThickness="5"></Rectangle> > </ControlTemplate> > </Button.Template> > </Button> > <Button Name="btn2"> > <Button.Template> > <ControlTemplate> > <Ellipse Fill="Green" Stroke="Violet" > Opacity="0.7" StrokeThickness="5"></Ellipse> > </ControlTemplate> > </Button.Template> > </Button> > </UniformGrid> > and the following code for the code behind > > private void UniformGrid_MouseDown(object sender, > MouseButtonEventArgs e) > { > FrameworkElement elm = e.OriginalSource as > FrameworkElement; > MessageBox.Show("You clicked " + elm.Name); > } > > Why is it that the events are never raised? I want to catch all button > mousedown events (ideally I would like to catch the click event for > buttons) in a global handler...how do i do this? As an alternative to the approach suggested by Pavel, you can see events even if they have already been handled. Try adding the line AddHandler(FrameworkElement.MouseDownEvent, new MouseButtonEventHandler(UniformGrid_MouseDown), true); to your window's constructor after the call to InitializeComponent (and remove "MouseDown="UniformGrid_MouseDown" from the XAML). Chris Jobson