WPF not firing events

I

Ilyas

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?
 
P

Pavel Minaev

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.
 
C

Chris Jobson

Ilyas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top