自定义Command

创建好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"/>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值