11、WPF_MVVM_Button按钮事件绑定

1、引用System.Windows.Interactivity.dll。安装“管理NuGet程序包”下载。
在这里插入图片描述
下载成功:
在这里插入图片描述
2、在“Common”文件夹,定义一个事件基类EventCommand.ca,继承TriggerAction<DependencyObject>,EventCommand.cs类代码如下:

using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace StudentMgrDemo.Common
{
    /// <summary>
    /// 事件命令
    /// </summary>
    public class EventCommand : TriggerAction<DependencyObject>
    {
        protected override void Invoke(object parameter)
        {
            if (CommandParameter != null)
            {
                parameter = CommandParameter;
            }
            if (Command != null)
            {
                Command.Execute(parameter);
            }
        }

        /// <summary>
        /// 事件
        /// </summary>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(EventCommand), new PropertyMetadata

(null));

        /// <summary>
        /// 事件参数,如果为空,将自动传入事件的真实参数
        /// </summary>
        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventCommand), new

PropertyMetadata(null));
    }
}


3、在LoginViewModel.cs中定义登录事件:

//定义登录事件
        private BaseCommand loginClick;
        public BaseCommand LoginClick
        {
            get
            {
                if (loginClick == null)
                {
                    loginClick = new BaseCommand(new Action<object>(o =>
                      {
                          //执行登陆的逻辑
                          MessageBox.Show("这是一个点击事件!");
                      }));
                }
                return loginClick;
            }
        }

4、引入命名空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:v="clr-namespace:StudentMgrDemo.Common"

在这里插入图片描述
在这里插入图片描述
5、PasswordBoxHelper.cs代码如下:

using System.Windows;
using System.Windows.Controls;

namespace StudentMgrDemo.Common
{
    public static class PasswordBoxHelper
    {
		public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached(
			"Password",
			typeof(string),
			typeof(PasswordBoxHelper),
			new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

		public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached(
			"Attach",
			typeof(bool),
			typeof(PasswordBoxHelper),
			new PropertyMetadata(false, Attach));

		private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached(
			"IsUpdating",
			typeof(bool),
			typeof(PasswordBoxHelper));

		public static void SetAttach(DependencyObject dp, bool value)
		{
			dp.SetValue(AttachProperty, value);
		}
		public static bool GetAttach(DependencyObject dp)
		{
			return (bool)dp.GetValue(AttachProperty);
		}
		public static string GetPassword(DependencyObject dp)
		{
			return (string)dp.GetValue(PasswordProperty);
		}
		public static void SetPassword(DependencyObject dp, string value)
		{
			dp.SetValue(PasswordProperty, value);
		}
		private static bool GetIsUpdating(DependencyObject dp)
		{
			return (bool)dp.GetValue(IsUpdatingProperty);
		}
		private static void SetIsUpdating(DependencyObject dp, bool value)
		{
			dp.SetValue(IsUpdatingProperty, value);
		}

		private static void OnPasswordPropertyChanged(
			DependencyObject sender,
			DependencyPropertyChangedEventArgs e)
		{
			PasswordBox passwordBox = sender as PasswordBox;
			passwordBox.PasswordChanged -= PasswordChanged;
			if (!(bool)GetIsUpdating(passwordBox))
			{
				passwordBox.Password = (string)e.NewValue;
			}
			passwordBox.PasswordChanged += PasswordChanged;
		}

		private static void Attach(
			DependencyObject sender,
			DependencyPropertyChangedEventArgs e)
		{
			PasswordBox passwordBox = sender as PasswordBox;
			if (passwordBox == null)
				return;
			if ((bool)e.OldValue)
			{
				passwordBox.PasswordChanged -= PasswordChanged;
			}
			if ((bool)e.NewValue)
			{
				passwordBox.PasswordChanged += PasswordChanged;
			}
		}

		private static void PasswordChanged(object sender, RoutedEventArgs e)
		{
			PasswordBox passwordBox = sender as PasswordBox;
			SetIsUpdating(passwordBox, true);
			SetPassword(passwordBox, passwordBox.Password);
			SetIsUpdating(passwordBox, false);
		}
	}
}


6、Login.xaml前端代码如下:

<!--事件绑定-->
            <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" 
                    Width="150" Height="40" FontSize="18" FontWeight="Bold" 
                    HorizontalAlignment="Center" VerticalAlignment="Top" 
                    Margin="0 5 0 0" Content="登 陆">
                <i:Interaction.Triggers>
                    <!--事件名称  EventName="Click"单机事件-->
                    <i:EventTrigger EventName="Click">
                        <v:EventCommand Command="{Binding LoginClick}"></v:EventCommand>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>

在这里插入图片描述
执行结果如下:
在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值