D
D
DotRun2017-10-11 15:33:38
WPF
DotRun, 2017-10-11 15:33:38

Why are routed events not firing?

Tell me why when you click on the ellipse outerEllipse after processing the PreviewMouseDown event, the rest of the handlers do not work? But if you remove MessageBox.Show(), then they work:

<StackPanel>
        <Button Name="btnClickMe" Height="75" Width = "250" Click ="btnClickMe_Clicked" >
            <StackPanel Orientation ="Horizontal">
                <Label Height="50" FontSize ="20">Fancy Button!</Label>
                <Canvas Height ="50" Width ="100" >
                    <Ellipse Name = "outerEllipse" Fill ="Green" Height ="25" Width ="50" Cursor="Hand" Canvas.Left="25" Canvas.Top="12" MouseDown="outerEllipse_MouseDown" PreviewMouseDown="outerEllipse_PreviewMouseDown"/>
                    <Ellipse Name = "innerEllipse" Fill ="Yellow" Height = "15" Width ="36" Canvas.Top="17" Canvas.Left="32"/>
                </Canvas>
            </StackPanel>
        </Button>
    </StackPanel>

public partial class WindowsApplication : Window
    {
        string data = string.Empty;

        public WindowsApplication()
        {
            InitializeComponent();
        }
        private void btnClickMe_Clicked(object sender, RoutedEventArgs e)
        {
            data += "Button Clicked\n";
            MessageBox.Show(data);
            data = string.Empty;
        }
        private void outerEllipse_MouseDown(object sender, MouseButtonEventArgs e)
        {
            data += "Outer MouseDown\n";
            MessageBox.Show(data);           
        }
        private void outerEllipse_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            data += "Outer PreviewMouseDown\n";
            MessageBox.Show(data);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DotRun, 2017-10-11
@DotRun

Found the answer:
Changing the focus to the message box cancels the MouseDown event.

The focus change to the message box cancels the mouse down event so it doesn't matter whether it is handled or not. Since you know which item the user was trying to select before you displayed the message box, simply select that item programmatically if the user presses YES.
© Rick Sweetkey

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question