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绑定

<Button Width="60" Height="30" Command="{Binding ShowMessage}"/>

(2) InputBindings.MouseBinding 鼠标事件绑定

例:

        <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 键盘事件绑定

<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

原文地址:https://www.cnblogs.com/yaosj/p/10948814.html