WPF中将事件包装成命令

WPF中将事件包装成命令在ViewModel中进行处理
1、需要用到的名称空间

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
该名称空间在在C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries\System.Windows.Interactivity.dll

2、XAMAL代码

<Window x:Class="CanDelete.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="self"
        xmlns:local="clr-namespace:CanDelete"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="MainWindow" Height="350" Width="525" >
    <Grid x:Name="MainGrid">
        <TextBox Margin="203,117,199,145" Name="textBox">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="KeyDown">
                    <i:InvokeCommandAction
                        Command="{Binding Path=DataContext.UnCheckedCommand, ElementName=self}" 
                        CommandParameter="{Binding ElementName=textBox}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
    </Grid>
</Window>

3、后端代码

namespace CanDelete
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MyViewModel();
        }
    }
//只实现上诉的功能的话可以不用继承NotifyableObject的,但是为了做个笔记所以把这个加上去了,这个是典型的MVVM模式中需要实现INotifyPropertyChanged
接口
    public class MyViewModel : NotifyableObject
    {
        public MyViewModel()
        {
            _unCheckedCommand = new RelayCommand(UnCheckedCommandHandler);
        }
        private ICommand _unCheckedCommand;

        public ICommand UnCheckedCommand
        {
            get { return _unCheckedCommand; }
            set 
            { 
                _unCheckedCommand = value; 
                RaisePropertyChanged(() => UnCheckedCommand); 
            }
        }

        public void UnCheckedCommandHandler(object para)
        {
            var txtBox = para as TextBox;
            txtBox.Text = txtBox.Text + "++++";
        }
    }

    public class RelayCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    }

    public class NotifyableObject : INotifyPropertyChanged
    {
        public string GetPropertyName<T>(Expression<Func<T>> extractionExpression)
        {
            return GetPropertyNameByExpression(extractionExpression);
        }

        protected void RaisePropertyChanged<T>(Expression<Func<T>> extractionExpression)
        {
            string name = GetPropertyName(extractionExpression);
            RaisePropertyChanged(name);
        }

        public string GetPropertyNameByExpression<T>(Expression<Func<T>> projection)
        {
            var exp = (projection.Body as MemberExpression);
            if (exp != null)
            {
                string e = exp.Member.Name;
                return e;
            }
            return null;
        }

        private void RaisePropertyChanged(string name)
        {
            IsDirty = true;
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值