gpt4 book ai didi

c# - 通过 Interactivity.EventTrigger 捕获自定义事件

转载 作者:太空狗 更新时间:2023-10-29 23:53:29 24 4
gpt4 key购买 nike

我有一个带有事件的简单用户控件:

using System.Windows;

namespace TriggersTest
{
public partial class MyControl
{
public MyControl()
{
InitializeComponent();
}

public static readonly RoutedEvent ButtonPressedEvent =
EventManager.RegisterRoutedEvent("ButtonPressed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyControl));

public event RoutedEventHandler ButtonPressed
{
add { AddHandler(ButtonPressedEvent, value); }
remove { RemoveHandler(ButtonPressedEvent, value); }
}

private void OnButtonClick(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs { RoutedEvent = ButtonPressedEvent });
}
}
}

我想在另一个 View 中捕捉这个事件:

<Window x:Class="TriggersTest.MainWindow"
Name="Root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:TriggersTest"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:MyControl>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ButtonPressed">
<i:InvokeCommandAction Command="{Binding MyCommand, ElementName=Root}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</local:MyControl>
</Grid>
</Window>

事件已引发,但i:EventTrigger 未调用命令。

我该如何解决这个问题?

最佳答案

您的代码一切正常。绑定(bind)不起作用。光看这个我就知道了。

不要在 InvokeCommandAction 中使用 ElementName,因为 InvokeCommandAction 不是 WPF 中 LogicalTree 的一部分。

只需更改您的绑定(bind)或使用 x:Reference,一切都应该没问题。

关于c# - 通过 Interactivity.EventTrigger 捕获自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19368233/

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