在 MSDN 文章Understanding Routed Events and Commands In WPF中,它指出
一个事件将从源元素向上冒泡(传播)到可视化树,直到它被处理或到达根元素。
但是,在此示例中,当您单击按钮时,它不会“冒泡视觉树”以由父 StackPanel 事件处理,即单击按钮不会触发任何事件。
为什么不?如果不是这个,他们所说的“冒泡”是什么意思?
XAML:
<Window x:Class="TestClickEvents456.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="TheStackPanel"
Background="Yellow"
MouseDown="TheStackPanel_MouseDown">
<Button x:Name="TheButton"
Margin="10"
Content="Click This"/>
<TextBlock x:Name="TheMessage"
Text="Click the button or the yellow area"/>
</StackPanel>
</Window>
代码隐藏:
using System.Windows;
using System.Windows.Input;
namespace TestClickEvents456
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void TheStackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
TheMessage.Text = "StackPanel was clicked.";
}
}
}