Pavel Minaev
7/3/2008 9:09:00 AM
On Jul 3, 11:41 am, Ilyas <il...@igsoftwaresolutions.co.uk> 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.