MVVM 事件转命令1
EventToCommand
在WPF中,并不是所有控件都有Command,例如TextBox,那么当文本改变,我们需要处理一些逻辑,这些逻辑在ViewModel
中,没有Command如何绑定呢?这个时候我们就用到EventToCommand,事件转命令,可以将一些事件例如TextChanged,Checked等
转换成命令的方式。
不带参数的EventToCommand
AppView.xaml
<CheckBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<command:EventToCommand Command="{Binding CheckedCommand}"></command:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<command:EventToCommand Command="{Binding UnCheckedCommand}"></command:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
AppViewModel.cs
public RelayCommand CheckedCommand
{
get;
set;
}
public RelayCommand UnCheckedCommand
{
get;
set;
}
public AppViewModel()
{
CheckedCommand = new RelayCommand(Checked);
UnCheckedCommand = new RelayCommand(UnChecked);
}
private void Checked()
{
MessageBox.Show("Checked");
}
private void UnChecked()
{
MessageBox.Show("Unchecked");
}
带参数的EventToCommand
AppView.xaml
<Button Width="100" Height="30" Margin="0,100,0,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<command:EventToCommand Command="{Binding ShowTxtCommand}" PassEventArgsToCommand="True" CommandParameter="Hello"></command:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
AppViewModel.cs
/// <summary>
/// 显示消息命令
/// </summary>
public RelayCommand<string> ShowTxtCommand
{
get;
set;
}
public AppViewModel()
{
ShowTxtCommand=new RelayCommand<string>(ShowMsg);
}
/// <summary>
/// 命令具体实现
/// </summary>
private void ShowMsg(string msg)
{
MessageBox.Show(msg);
}
转换传递的参数
也许你想知道事件是谁触发的,从而进行一些业务逻辑,那么我们需要从IEventArgsConverter
继承
EventArgsConverter.cs
public class EventArgsConverter : IEventArgsConverter
{
public object Convert(object value, object parameter)
{
var args = (RoutedEventArgs)value;
var btn = parameter as Button;
return btn.Name;
}
}
AppView.xaml
<Button Width="100" Height="30" Margin="0,100,0,0" x:Name="btn">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<command:EventToCommand Command="{Binding ShowNameCommand}" EventArgsConverter="{StaticResource ArgsConverter}" EventArgsConverterParameter="{Binding ElementName=btn}" PassEventArgsToCommand="True"></command:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
AppViewModel.cs
public RelayCommand<string> ShowNameCommand
{
get;
set;
}
public AppViewModel()
{
ShowNameCommand = new RelayCommand<string>(ShowName);
}
private void ShowName(string name)
{
MessageBox.Show(name);
}
看了EventToCommand的源码,并没有什么帮助,我猜测它的实现是这样的,EventToCommand属于附加
属性,它就能获取到它所附加的对象即控件本身,然后它还能Binding命令,比如我们附加的对象是Button
,那么就在它的Click事件时,触发Command命令,同样,有条件判断Command是否能执行时,可以设置
控件的IsEnabled
属性。
顶
收藏
关注
评论
作者:王思明
出处:http://www.cnblogs.com/maanshancss/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。所有源码遵循Apache协议,使用必须添加 from maanshancss
出处:http://www.cnblogs.com/maanshancss/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。所有源码遵循Apache协议,使用必须添加 from maanshancss
分类:
桌面开发 WPF MVVM
标签:
MVVM
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战