gpt4 book ai didi

c# - EventTrigger RoutedEvent 未使用自定义事件触发

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:42 25 4
gpt4 key购买 nike

我正在尝试在触发自定义路由事件(由我定义)时触发 Storyboard播放。该事件称为 CustomTest 并在 MyControl 中定义。

虽然正在触发事件,但触发器并未播放 Storyboard 。

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,我可以看到事件一直在我的控制之下(处理的是“False”)。但是,触发器实际上并未启动 Storyboard。

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

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

最佳答案

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

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

关于c# - EventTrigger RoutedEvent 未使用自定义事件触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22336231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com