[英]Catch custom event by Interactivity.EventTrigger
I have a simple user-control with an event:我有一个带有事件的简单用户控件:
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 });
}
}
}
And I want to catch this event in the other 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>
The event has been raised, but i:EventTrigger doesn't invoke a command.事件已引发,但i:EventTrigger不调用命令。
How can I fix this?我该如何解决这个问题?
Its all fine with your code.您的代码一切正常。 The binding is not working.
绑定不起作用。 I can tell just by looking at this.
光看这个我就知道了。
Do not use ElementName inside InvokeCommandAction because InvokeCommandAction is not part of LogicalTree in WPF.不要在 InvokeCommandAction 中使用 ElementName,因为 InvokeCommandAction 不是 WPF 中 LogicalTree 的一部分。
Just change your Binding or use x:Reference and everything should be fine.只需更改您的绑定或使用 x:Reference 一切都应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.