Skip to content
分类目录:

Behavior

Post date:
Author:

可以通过代码的方法给控件增加一些行为,并直接在XAML中使用该逻辑。

Reference

.Net Framework项目中添加的引用:

Add a reference to the System.Windows.Interactivity assembly in your WPF project.

Xaml中需要引用的namespace:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

.Net core项目中需要添加的

<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.31" />

xaml中添加的namespace:

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

Define

定义Behavior

// If ==.NetFramework
using System.Windows.Interactivity;
// If >= Net6.0
using Microsoft.Xaml.Behaviors;

using System.Windows.Controls;

public class MyBehavior : Behavior<Button>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        // Add your behavior logic here
        AssociatedObject.Click += AssociatedObject_Click;
    }

    private void AssociatedObject_Click(object sender, RoutedEventArgs e)
    {
        // Handle the button click event
    }
}

Use

<Button Content="Click me">
    <i:Interaction.Behaviors>
        <local:MyBehavior />
    </i:Interaction.Behaviors>
</Button>

Password绑定:

WPF中PasswordBox控件的password无法使用绑定,需要借助一些其他办法:

public class PasswordAttachedHelper
{
    public static string GetPasswordText(DependencyObject obj)
    {
        return (string)obj.GetValue(PasswordTextProperty);
    }

    public static void SetPasswordText(DependencyObject obj, string value)
    {
        obj.SetValue(PasswordTextProperty, value);
    }

    // Using a DependencyProperty as the backing store for PasswordText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PasswordTextProperty =
        DependencyProperty.RegisterAttached("PasswordText", typeof(string), typeof(PasswordAttachedHelper), new PropertyMetadata(string.Empty, OnChangedCallBack));

    private static void OnChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var passwordBotControl = d as PasswordBox;
        if (passwordBotControl == null)
        {
            return;
        }
        if (e.NewValue == null)
        {
            return;
        }
        if (e.OldValue == e.NewValue)
        {
            return;
        }
        passwordBotControl.Password = e.NewValue.ToString();
    }
}
public class PasswordBehavior : Behavior<PasswordBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.PasswordChanged -= AssociatedObject_PasswordChanged;
        this.AssociatedObject.PasswordChanged += AssociatedObject_PasswordChanged;
    }

    private void AssociatedObject_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
    {
        PasswordAttachedHelper.SetPasswordText(this.AssociatedObject, this.AssociatedObject.Password);
    }
}
<PasswordBox attach:PasswordAttachedHelper.PasswordText="{Binding Token,Mode=TwoWay}" MinWidth="320" >
    <i:Interaction.Behaviors>
        <behavior:PasswordBehavior/>
    </i:Interaction.Behaviors>
</PasswordBox>
豫ICP备2021008859号-1