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.";
}
}
}