2
我有以下路由事件:處理窗口的RoutedEvent在許多WPF元素與風格
public static readonly RoutedEvent FakeEvent = EventManager.RegisterRoutedEvent(
"Fake", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(MainWindow));
public event RoutedEventHandler Fake
{
add { AddHandler(FakeEvent, value); }
remove { RemoveHandler(FakeEvent, value); }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(MainWindow.FakeEvent);
RaiseEvent(newEventArgs);
}
我有以下XAML:
<Window.Resources>
<Style TargetType="{x:Type TextBlock}"
xmlns:local="clr-namespace:WpfApplication1">
<Setter Property="Margin" Value="10" />
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<EventTrigger RoutedEvent="local:MainWindow.Fake">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Blue" Duration="0:0:1"
Storyboard.TargetProperty="Background.Color" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<Button Click="Button_Click">Raise Event</Button>
<TextBlock>Hello World</TextBlock>
<TextBlock>Hello World</TextBlock>
<TextBlock>Hello World</TextBlock>
<TextBlock>Hello World</TextBlock>
<TextBlock>Hello World</TextBlock>
<TextBlock>Hello World</TextBlock>
</StackPanel>
我的目標是,窗口的路由事件將導致故事板通過使用可重用的通用樣式來觸發所有TextBlocks。但是,引發路由事件(通過單擊按鈕)不會產生任何結果(沒有錯誤,只是沒有)。不知道有什麼問題。
什麼是正確的方法呢?