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

 

posted @   翻白眼的哈士奇  阅读(2770)  评论(0编辑  收藏  举报
编辑推荐:
· 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搭建本
点击右上角即可分享
微信分享提示