C# 窗口可以';t处理从已初始化事件处理程序中引发的路由事件
我有一个已经向用户显示的主窗口。在该窗口中有一个按钮,单击该按钮时,将加载一个由包含文本块的列表框组成的UserControl。这些文本块在事件处理程序中为其初始化和加载的事件引发路由事件(即SomeEvent)。但是,只有在TextBlock.Loaded上引发的事件会在主窗口的可视树中出现气泡,而在TextBlock.Initialized上引发的事件不会出现气泡 考虑以下示例项目: main window.xamlC# 窗口可以';t处理从已初始化事件处理程序中引发的路由事件,c#,wpf,xaml,C#,Wpf,Xaml,我有一个已经向用户显示的主窗口。在该窗口中有一个按钮,单击该按钮时,将加载一个由包含文本块的列表框组成的UserControl。这些文本块在事件处理程序中为其初始化和加载的事件引发路由事件(即SomeEvent)。但是,只有在TextBlock.Loaded上引发的事件会在主窗口的可视树中出现气泡,而在TextBlock.Initialized上引发的事件不会出现气泡 考虑以下示例项目: main window.xaml <Window x:Class="RoutedEventsExamp
<Window x:Class="RoutedEventsExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock x:Name="ui_mainWindowLoadStatus" />
<StackPanel Orientation="Horizontal" Margin="0,20,0,0">
<TextBlock Text="Items Initialized:" />
<TextBlock x:Name="ui_initializedCount" Margin="10,0,0,0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,20,0,0">
<TextBlock Text="Items Loaded:" />
<TextBlock x:Name="ui_loadedCount" Margin="10,0,0,0" />
</StackPanel>
<Button Click="Button_OnClick" Content="Display Items" Margin="0,20,0,0" />
<Border x:Name="ui_host" Margin="0,20,0,0" />
</StackPanel>
</Window>
<UserControl x:Class="RoutedEventsExample.Items"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:routedEventsExample="clr-namespace:RoutedEventsExample"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ListBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type routedEventsExample:Items}}, Path=ItemsSource}"
VirtualizingStackPanel.IsVirtualizing="False">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type system:String}">
<routedEventsExample:Item />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
<UserControl x:Class="RoutedEventsExample.Item"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
d:DataContext="{d:DesignInstance system:String}">
<TextBlock Text="{Binding}" Initialized="TextBlock_OnInitialized" Loaded="TextBlock_OnLoaded" />
</UserControl>
Items.xaml
<Window x:Class="RoutedEventsExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock x:Name="ui_mainWindowLoadStatus" />
<StackPanel Orientation="Horizontal" Margin="0,20,0,0">
<TextBlock Text="Items Initialized:" />
<TextBlock x:Name="ui_initializedCount" Margin="10,0,0,0" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,20,0,0">
<TextBlock Text="Items Loaded:" />
<TextBlock x:Name="ui_loadedCount" Margin="10,0,0,0" />
</StackPanel>
<Button Click="Button_OnClick" Content="Display Items" Margin="0,20,0,0" />
<Border x:Name="ui_host" Margin="0,20,0,0" />
</StackPanel>
</Window>
<UserControl x:Class="RoutedEventsExample.Items"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:routedEventsExample="clr-namespace:RoutedEventsExample"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ListBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type routedEventsExample:Items}}, Path=ItemsSource}"
VirtualizingStackPanel.IsVirtualizing="False">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type system:String}">
<routedEventsExample:Item />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
<UserControl x:Class="RoutedEventsExample.Item"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
d:DataContext="{d:DesignInstance system:String}">
<TextBlock Text="{Binding}" Initialized="TextBlock_OnInitialized" Loaded="TextBlock_OnLoaded" />
</UserControl>
有人能解释为什么从TextBlock的初始化处理程序引发的“某些”事件不由MainWindow处理,而从TextBlock的加载处理程序引发的“某些”事件由MainWindow处理