【问题标题】:EventTrigger RoutedEvent not triggered with Custom Event自定义事件未触发 EventTrigger RoutedEvent
【发布时间】:2014-03-11 20:44:13
【问题描述】:

我试图在触发自定义路由事件(由我定义)时触发故事板播放。该事件称为 CustomTest,并在 MyControl 中定义。

虽然触发了事件,但触发器并未播放情节提要。

XAML:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:gear="clr-namespace:GearWPF"
    x:Class="GearWPF.MyControl"
    x:Name="UserControl">

...

    <Grid x:Name="LayoutRoot" Background="#00000000">
        <Grid.Triggers>
            <EventTrigger RoutedEvent="gear:MyControl.CustomTest"> <!-- My custom event. -->
                <BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
            </EventTrigger>
        </Grid.Triggers>
    </Grid>

C#:

namespace GearWPF
{
    /// <summary>
    /// Interaction logic for MyControl.xaml
    /// </summary>
    public partial class MyControl: UserControl
    {

        // Create a custom routed event by first registering a RoutedEventID 
        // This event uses the bubbling routing strategy 
        public static readonly RoutedEvent CustomTestEvent = EventManager.RegisterRoutedEvent("CustomTest", RoutingStrategy.Bubbling, typeof(RoutedEventHandler), typeof(MyControl));

        // Provide CLR accessors for the event 
        public event RoutedEventHandler CustomTest
        {
            add { AddHandler(CustomTestEvent, value); }
            remove { RemoveHandler(CustomTestEvent, value); }
        }

        public MyControl()
        {
            this.InitializeComponent();
        }

        public void RaiseMyEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomTestEvent);
            RaiseEvent(newEventArgs);
        }
    }
}

我已经确认 RaiseMyEvent 在我期望的时候被调用。并且使用 Snoop,我可以看到该事件一直到我的控制(其中处理的是“假”)。但是,触发器实际上并未启动情节提要。

我还更改了触发器以使用现有事件,当我这样做时,情节提要被触发。这让我相信它是特定于我的路由事件 CustomTest。

    <Grid x:Name="LayoutRoot" Background="#00000000">
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Mouse.MouseEnter"> <!-- This works! -->
                <BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
            </EventTrigger>
        </Grid.Triggers>
    </Grid>

【问题讨论】:

    标签: c# wpf user-controls triggers routed-events


    【解决方案1】:

    问题在于该事件的提升级别太高。我将它发送到我的 CustomControl,但我真正想做的是将它发送到我的网格 within CustomControl(因为事件只在根和源之间传递)。

        public void RaiseMyEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomTestEvent);
            LayoutRoot.RaiseEvent(newEventArgs); // This change fixes the issue.
        }
    

    【讨论】:

    • 我的 MainWindow.xaml.cs 中有自己的 RoutedEvent,但 EventTrigger 没有触发。你的回答对我有帮助,因为我把它放在我的网格里,而且太低了,无法接受这个事件!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多