使用 Style 处理许多 WPF 元素中的 Window's RoutedEvent
我有以下路由事件:
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>
我的目标是窗口的路由事件将导致故事板通过使用可重用的通用样式为所有 TextBlock 触发。但是,引发路由事件(通过单击按钮)不会导致任何结果发生(没有错误,只是没有任何结果)。不知道出了什么问题。
对此正确的做法是什么?
I have the following routed event:
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);
}
I have the following 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>
My goal is that the window's routed event would cause the storyboard to fire for all the TextBlocks by using a reusable, generic style. However, raising the routed event (by clicking the button) results in nothing occurring (no error, just nothing). Not sure what's wrong.
What is the correct approach to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能误解了隧道的工作原理:
这里事件将从根(窗口)传输到源(也是窗口),它永远不会遇到
TextBlocks
。您需要在所有这些事件上引发事件或侦听窗口上的事件,遗憾的是您无法在样式中使用EventTrigger.SourceName
。遗憾的是,我不知道对此有什么好的解决方案...(您可以使用
EventSetter
来处理TextBlocks
Loaded 事件code> 然后监听窗口上的事件并在本地重新引发它(您将需要更改路由策略,否则如果您不检查事件来自何处,您将得到堆栈溢出异常),这可能是有问题的如果这是个好主意)You might have misunderstood how tunneling works:
Here the event will travel from the root, the window, to the source, also the window, it will never come across the
TextBlocks
. You would either need to raise the event on all of them or listen to the event on the window, unfortunately you cannot useEventTrigger.SourceName
in styles. Sadly i do not know of any good solution to this...(You could use an
EventSetter
to handle theLoaded
event of theTextBlocks
to then listen to the event on the window and re-raise it locally (you will want to change the routing strategy or you will get a stack overflow exception if you do not check where the event came from), it might be questionable if that is a good idea)