使用 Style 处理许多 WPF 元素中的 Window's RoutedEvent

发布于 2025-01-04 15:40:41 字数 1859 浏览 0 评论 0原文

我有以下路由事件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么敢 2025-01-11 15:40:41

您可能误解了隧道的工作原理:

隧道:最初,调用元素树根处的事件处理程序。然后,路由事件沿着路线穿过连续的子元素,到达作为路由事件源的节点元素(引发路由事件的元素)。

这里事件将从根(窗口)传输到源(也是窗口),它永远不会遇到 TextBlocks。您需要在所有这些事件上引发事件或侦听窗口上的事件,遗憾的是您无法在样式中使用 EventTrigger.SourceName 。遗憾的是,我不知道对此有什么好的解决方案...

(您可以使用 EventSetter 来处理 TextBlocksLoaded 事件code> 然后监听窗口上的事件并在本地重新引发它(您将需要更改路由策略,否则如果您不检查事件来自何处,您将得到堆栈溢出异常),这可能是有问题的如果这是个好主意)

You might have misunderstood how tunneling works:

Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event).

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 use EventTrigger.SourceName in styles. Sadly i do not know of any good solution to this...

(You could use an EventSetter to handle the Loaded event of the TextBlocks 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文