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
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    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(Expression> extractionExpression)
        {
            return GetPropertyNameByExpression(extractionExpression);
        }

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

        public string GetPropertyNameByExpression(Expression> 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 { };

    }
}

你可能感兴趣的:(WPF(MVVM),WPF)