简体   繁体   English

如何使用Interactivity.EventTrigger捕获自定义事件

[英]How to catch a custom event with Interactivity.EventTrigger

I'm trying to catch my custom event (TextUpdateEvent) with Interactivity.EventTrigger in the way shown below. 我正在尝试通过Interactivity.EventTrigger捕获我的自定义事件(TextUpdateEvent),如下所示。 But this code gives rise to System.ArgumentException with the message that "The event "MyTextBox" was not found in the type "tool:MyTextBox.TextUpdateEvent"." 但是,此代码会引发System.ArgumentException,并显示以下消息:“在类型“ tool:MyTextBox.TextUpdateEvent”中未找到事件“ MyTextBox”。 Could you tell me what is wrong with this? 你能告诉我这是怎么回事吗?

<UserControl x:Class="mynamespace.MyControl"
xmlns:local="clr-namespace:mynamespace"
xmlns:info="clr-namespace:mynamespace.info"
xmlns:tool="clr-namespace:mynamespace.tool"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
<Grid>
    <Border>
        <ItemsControl ItemsSource="{Binding MyItems}" Name="itemsControl">
            <ItemsControl.ItemTemplate>                         
                <DataTemplate DataType="info:MyInfo">                             
                    <StackPanel Orientation="Horizontal">                                  
                        <TextBlock Text="{Binding MyComment}"/>
                        <tool:MyTextBox Text="{Binding MyName}" x:Name="myTextBox">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="tool:MyTextBox.TextUpdateEvent"
                                                SourceObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type tool:MyTextBox}}}"
                                                SourceName="myTextBox">                         
                                    <ei:CallMethodAction MethodName="UpdateMyInfo" TargetObject="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Mode=TwoWay}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </tool:MyTextBox>
                    </StackPanel>                              
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Border>     
</Grid>

namespace mynamespace.tool
{
    //used to judge whether or not the update suceeded
    public delegate bool MyTextBoxUpdateHandler(object sender);

    public partial class MyTextBox : TextBox
    {
        public MyTextBoxUpdateHandler TextUpdateEvent { get; set; }
    }
}

Or, do I need to create a custom trigger to catch my custom event? 还是我需要创建一个自定义触发器来捕获我的自定义事件?

public class TextUpdateEventTrigger : EventTriggerBase<MyTextBox>
{
    // I don't know what I should do here,
    // since the event I'd like to catch is not a RoutedEvent.
}

You should change the TextUpdateEvent to an event instead of a property. 您应该将TextUpdateEvent更改为事件而不是属性。

public event MyTextBoxUpdateHandler TextUpdateEvent;

Since you're calling a method in code-behind you also do not need to attach Interaction triggers: 由于您是在代码后方调用方法,因此您也不需要附加Interaction触发器:

<tool:MyTextBox 
    x:Name="myTextBox" 
    Text="{Binding MyName}" 
    TextUpdateEvent="UpdateMyInfo" />

The DataContext Property does not need to be bound on the UserControl itself, because it's already a partial class of the UserControl , you can remove that too. DataContext属性不需要绑定到UserControl本身,因为它已经是UserControl的局部类,您也可以删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM