WPF绑定命令
一、目的
降低代码耦合度(降低UI层和BLL层的代码耦合度),将UI层的后台代码更好的转移到BLL层中,让视图和业务逻辑分离的更好
二、使用方式
1.创建一个RelayCommand,继承ICommand接口
public class RelayCommand : ICommand { #region Fields private readonly Func<Object, Boolean> _canExecute; private readonly Action<Object> _execute; #endregion #region Constructors public RelayCommand(Action<Object> execute) : this(execute, null) { } public RelayCommand(Action<Object> execute, Func<Object, Boolean> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; _canExecute = canExecute; } #endregion #region ICommand Members public event EventHandler CanExecuteChanged { add { if (_canExecute != null) CommandManager.RequerySuggested += value; } remove { if (_canExecute != null) CommandManager.RequerySuggested -= value; } } public Boolean CanExecute(Object parameter) { return _canExecute == null ? true : _canExecute(parameter); } public void Execute(Object parameter) { _execute(parameter); } #endregion }
2.创建一个ViewModel类,创建RelayCommand属性对象
(1)使用lambda表达式
public class ViewModelTest { public ICommand ShowMessage { get { return new RelayCommand(new Action<Object>(t => { if (t == null) MessageBox.Show("not have param"); else MessageBox.Show(t.ToString()); })); } } }
(2)使用函数
public class ViewModelTest { private void UpdateNameExecute(Object parameter) { MessageBox.Show("haha"); } private bool CanUpdateNameExecute(Object parameter) { return true; } public ICommand ShowMessage { get { return new RelayCommand(UpdateNameExecute, CanUpdateNameExecute); } } }
3.界面后台类,将ViewModel对象赋给内容上下文
DataContext = new ViewModelTest();
4.界面绑定命名
(1)不带参数
<Button Width="60" Height="30" Command="{Binding ShowMessage}"/>
(2)带参数
<Button Width="60" Height="30" Command="{Binding ShowMessage}" CommandParameter="have param"/>
5.绑定命令的方式
(1) button类型的按钮 直接用Command绑定
1 | <Button Width= "60" Height= "30" Command= "{Binding ShowMessage}" /> |
(2) InputBindings.MouseBinding 鼠标事件绑定
例:
1 2 3 4 5 | <Label Content= "点击我呀" HorizontalAlignment= "Left" Height= "23" Margin= "243,256,0,0" VerticalAlignment= "Top" Width= "120" > <Label.InputBindings> <MouseBinding MouseAction= "LeftClick" Command= "{Binding CmdShow}" ></MouseBinding> </Label.InputBindings> </Label> |
MouseAction:
// 不执行任何操作。
None = 0,
// 单击鼠标左键。
LeftClick = 1,
// 单击鼠标右键。
RightClick = 2,
// 单击鼠标按钮。
MiddleClick = 3,
// 单次鼠标轮旋转。
WheelClick = 4,
// 双击鼠标左键。
LeftDoubleClick = 5,
// 双击鼠标右键。
RightDoubleClick = 6,
// 双击鼠标按钮。
MiddleDoubleClick = 7
(3)InputBindings.KeyBinding 键盘事件绑定
1 2 3 4 5 6 7 8 | <Window.InputBindings> //按键A <KeyBinding Key= "A" Command= "{Binding CmdShow}" /> //按键Ctrl + B <KeyBinding Gesture= "Ctrl + B" Command= "{Binding CmdShow}" /> //按键Shift+C <KeyBinding Modifiers= "Shift" Key= "C" Command= "{Binding CmdShow}" /> </Window.InputBindings> |
key(enum Key):单个按键
Gesture:组合按键
Modeifers(enum ModifierKeys):指定修改键集 和key组合使用
public enum ModifierKeys
{
// 按下没有任何修饰符。
None = 0,
// ALT 键。
Alt = 1,
// CTRL 键。
Control = 2,
// SHIFT 键。
Shift = 4,
// Windows 徽标键。
Windows = 8
}
参考:
https://www.cnblogs.com/weiweiboqi/p/4682136.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本