自定义Command

来源:互联网 发布:jython python 区别 编辑:程序博客网 时间:2024/05/17 22:47

创建好Command,CommandSource和一个接口类型。

 public interface IClearControl    {        bool IsChanged { get; }        void Clear();    }    public class ClearCommand : ICommand    {        public bool CanExecute(object parameter)        {            IClearControl o = parameter as IClearControl;            if (o != null && o.IsChanged)            {                return true;            }            return false;        }        public event EventHandler CanExecuteChanged;        public void Execute(object parameter)        {            IClearControl o = parameter as IClearControl;            if (o != null)            {                o.Clear();            }        }    }    public class CustomControl : UserControl, ICommandSource    {        public ICommand Command        {            get;            set;        }        public object CommandParameter        {            get;            set;        }        public IInputElement CommandTarget        {            get;            set;        }        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)        {            base.OnMouseLeftButtonDown(e);            if (Command != null && CommandTarget != null && Command.CanExecute(CommandTarget))            {                Command.Execute(CommandTarget);            }        }    }

创建一个用户控件,实现上面创建的接口

 public partial class UCClear : UserControl, IClearControl    {        public UCClear()        {            InitializeComponent();        }        public bool IsChanged        {            get            {                if (string.IsNullOrEmpty(txt.Text))                {                    return false;                }                return true;            }        }        public void Clear()        {            txt.Text = txt.Text.Remove(txt.SelectionStart - 1);            txt.SelectionStart = txt.Text.Length;        }    }

前台XAML

<UserControl x:Class="DeepWPF.UCClear"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"            Background="AliceBlue"            >    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" >        <TextBlock Text="点击一次清除一个字符" HorizontalAlignment="Center"                   FontSize="16" Margin="2" Background="BlanchedAlmond"/>        <TextBox Name="txt"  Height="31" BorderBrush="Bisque"/>    </StackPanel></UserControl>


接着在主界面中嵌入控件

 <local:CustomControl x:Name="cmdSource">            <local:UCClear x:Name="ucCtrl"/> </local:CustomControl>

在后台代码里设置好命令(比如主界面的Loaded事件中)

 ClearCommand clearCmd = new ClearCommand(); cmdSource.Command = clearCmd; cmdSource.CommandTarget = ucCtrl;


现在运行程序点击一次会删除TextBox中的一个字符,没有字符则不执行任何操作。


如果把点击的TextBlock改成Button,且当TextBox中没有字符的时候Button不可用,有值的时候才能点击执行命令

需要把代码改成如下:

 public class ClearCommand : ICommand    {        public bool CanExecute(object parameter)        {            IClearControl o = parameter as IClearControl;            if (o != null && o.IsChanged)            {                return true;            }            return false;        }        public event EventHandler CanExecuteChanged        {            add { CommandManager.RequerySuggested += value; }            remove { CommandManager.RequerySuggested -= value; }        }        public void Execute(object parameter)        {            IClearControl o = parameter as IClearControl;            if (o != null)            {                o.Clear();            }        }    }    public class CustomControl : UserControl, ICommandSource    {        void ChangedIsEnabled(object sender, EventArgs e)        {            this.IsEnabled = command.CanExecute(this.CommandTarget);        }        private ICommand command;        public ICommand Command        {            get { return command; }            set            {                command = value;                //如果有多次赋值则需要清除前面的注册,否则会内存泄漏                command.CanExecuteChanged -= ChangedIsEnabled;                command.CanExecuteChanged += ChangedIsEnabled;            }        }        public object CommandParameter        {            get;            set;        }        public IInputElement CommandTarget        {            get;            set;        }                protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)        {            base.OnPreviewMouseLeftButtonDown(e);            if (CommandTarget != null && Command.CanExecute(CommandTarget))            {                Command.Execute(CommandTarget);            }            e.Handled = true;        }    }


把OnMouseLeftButtonDown事件改成OnPreviewMouseLeftButtonDown是因为Button的Click会导致OnMouseLeftButtonDown不被执行,所以需要换成OnPreviewMouseLeftButtonDown事件,前台代码改成Button,UCClear控件中把TextBlock去掉(没用了改成Button了)其他代码不变。

<local:CustomControl x:Name="cmdSource">     <Button Width="89" Height="31" HorizontalAlignment="Left" Content="点击清除" /></local:CustomControl><local:UCClear x:Name="ucCtrl"/>



原创粉丝点击