自定义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;
如果把点击的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"/>
- 自定义Command
- 自定义Command
- WPF自定义命令Command
- 自定义Django Command命令
- 自定义Django Command命令
- django自定义command命令
- Silverlight中的Command,自定义简单Command
- OSGI中自定义command(2)
- Wpf 自定义绑定Command和手动更新Command状态
- WPF 自定义快捷键命令(Command)(转)
- GEF-自定义Request、Command与Action
- # OSGI中使用自定义的command
- WPF 自定义快捷键命令(Command)
- Command
- Command
- Command
- command
- Command
- mysql双机热备问题
- 什么是CSS的盒子模型
- javascript 用出生日期计算现在年龄的小代码
- ARM流水线
- 从“精益创业”思考创业切入点
- 自定义Command
- 小笨孩工作记(二)
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing……
- GMF中,禁止delete from diagram
- 持续集成:软件质量改进和风险降低之道
- Selenium IDE的使用
- Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
- W3C规范——表单提交
- 我为什么又选择了Windows Phone