WPF-后台代码使用Behavior

xaml里面使用很简单

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"


 <i:Interaction.Behaviors>
    <i:MouseDragElementBehavior/>
 </i:Interaction.Behaviors>

 

后台代码使用

Microsoft.Xaml.Behaviors.Layout.MouseDragElementBehavior mouseDragElementBehavior = new Microsoft.Xaml.Behaviors.Layout.MouseDragElementBehavior();
Microsoft.Xaml.Behaviors.Interaction.GetBehaviors(ElementName).Add(mouseDragElementBehavior);

 

 

加上触发器

xaml

<TextBox x:Name="TextBoxInvoker" Text="TextBox" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyDown" >
            <behavior:TextBoxEnterButtonInvoke TargetName="TargetedButton" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

后台代码

TextBoxEnterButtonInvoke textBoxEnterButtonInvoke = new TextBoxEnterButtonInvoke();
textBoxEnterButtonInvoke.TargetName = "TargetedButton";
 
System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("KeyDown");
eventTrigger.Actions.Add(textBoxEnterButtonInvoke);
 
System.Windows.Interactivity.Interaction.GetTriggers(TextBoxInvoker).Add(eventTrigger);

 

posted @ 2020-07-13 19:23  当年小清新  阅读(190)  评论(0编辑  收藏