Version 1 XAML code:
<Button Padding="10" Background="Green" Width="500">
<StackPanel MouseDown="StackPanel_MouseDown" Background="Gray" Width="500" Height="300">
<Ellipse MouseDown="Ellipse_MouseDown" Width="100" Height="100" Fill="Red"></Ellipse>
</StackPanel>
</Button>
Version 1 Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("This is the Ellipse");
// Shows this MessageBox.
}
private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("This is the StackPanel");
// Also shows this MessageBox.
}
}
When I have 2 MouseDown
events, one for the Ellipse
and one for the StackPanel
, whether I use MessageBox
or not, it still bubbles up fine after showing the first MessageBox
.
However, it stops working if I have the Click
event on the Button
.
Version 2 XAML Code:
<Button Padding="10" Background="Green" Width="500" Click="Button_Click">
<StackPanel Background="Gray" Width="500" Height="300">
<Ellipse MouseDown="Ellipse_MouseDown" Width="100" Height="100" Fill="Red"></Ellipse>
</StackPanel>
</Button>
Version 2 Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("This is the Ellipse");
// Shows this
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("This is the Button");
// Doesn't get to this point.
}
}
Now, the weird thing is, if I remove the MessageBox
code in the Ellipse
handler from Versio n2, and put anything else, it will then bubble up to the Button
's handler and show the "This is the Button" message.
What is this weird behavior? Why does Version 1 bubble up and show BOTH MessageBox
es but Version2 only shows one MessageBox
and doesn't bubble up unless I remove the MessageBox
code?