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>
执行结果如下:
版权声明:本文为weixin_44560027原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。