public partial class MyUserControl : UserControl,ICommandSource{ public MyUserControl() { InitializeComponent(); } public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty,value); } } public static Readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command",typeof(ICommand),typeof(MyUserControl),new UIPropertyMetadata(null)); public object CommandParameter { get { return (object)GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty,value); } } // Using a DependencyProperty as the backing store for CommandParameter. This enables animation,styling,binding,etc... public static Readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter",typeof(object),new UIPropertyMetadata(null)); public IinputElement CommandTarget { get { return (IinputElement)GetValue(CommandTargetProperty); } set { SetValue(CommandTargetProperty,value); } } // Using a DependencyProperty as the backing store for CommandTarget. This enables animation,etc... public static Readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget",typeof(IinputElement),new UIPropertyMetadata(null)); protected overrIDe voID OnMouseleftbuttonUp(MousebuttonEventArgs e) { base.OnMouseleftbuttonUp(e); var command = Command; var parameter = CommandParameter; var target = CommandTarget; var routedCmd = command as RoutedCommand; if (routedCmd != null && routedCmd.CanExecute(parameter,target)) { routedCmd.Execute(parameter,target); } else if (command != null && command.CanExecute(parameter)) { command.Execute(parameter); } }}
请注意,CommandTarget属性仅用于RoutedCommands
总结以上是内存溢出为你收集整理的c# – 在WPF中如何实现ICommandSource以使我的自定义控件能够使用来自xaml的Command?全部内容,希望文章能够帮你解决c# – 在WPF中如何实现ICommandSource以使我的自定义控件能够使用来自xaml的Command?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)