zoukankan      html  css  js  c++  java
  • WPF 如何:实现 ICommandSource

    本人实在是太懒了,实在是不想引用DLL
    所以自定义 MyTextBlock , 使用其MouseLeftClick来触发ICommand

    namespace MyControls20210528  {
        class MyTextBlock : TextBlock, ICommandSource
        {
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.Register("Command", typeof(ICommand), typeof(MyTextBlock),
                    new PropertyMetadata((ICommand)null,new PropertyChangedCallback(CommandChanged)));
    
            public static readonly DependencyProperty CommandParameterProperty = 
                DependencyProperty.Register("CommandParameter", typeof(object), typeof(MyTextBlock));
    
            public static readonly DependencyProperty CommandTargetProperty = 
                DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(MyTextBlock));
    
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
    
            public object CommandParameter
            {
                get { return GetValue(CommandParameterProperty); }
                set { SetValue(CommandParameterProperty, value); }
            }
    
            public IInputElement CommandTarget
            {
                get { return (IInputElement)GetValue(CommandTargetProperty); }
                set { SetValue(CommandTargetProperty, value); }
            }
    
            private static void CommandChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
            {
                MyTextBlock myTxtb = d as MyTextBlock;
                if(myTxtb!=null)
                {
                    ICommand oldCommand = e.OldValue as ICommand;
                    ICommand newCommand = e.NewValue as ICommand;
                    myTxtb.HookUpCommand(oldCommand, newCommand);
                }
            }
            private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
            {
                if (oldCommand != null)
                {
                    oldCommand.CanExecuteChanged -= CanExecuteChanged;
                }
                if (newCommand != null)
                {
                    newCommand.CanExecuteChanged += CanExecuteChanged;
                }
            }
    
            private void CanExecuteChanged(object sender, EventArgs e)
            {
                RoutedCommand command = this.Command as RoutedCommand;
                if (command != null)
                {
                    this.IsEnabled = command.CanExecute(CommandParameter, CommandTarget);
                }
                else if (this.Command != null)
                {
                    this.IsEnabled = this.Command.CanExecute(CommandParameter);
                }
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                base.OnMouseLeftButtonDown(e);
                RoutedCommand command = Command as RoutedCommand;
                if (command != null)
                    command.Execute(CommandParameter, CommandTarget);
                else if (Command != null)
                    this.Command.Execute(CommandParameter);
            }
        }
    }
    

    xaml中如何使用:

    xmlns:MyTxtb ="clr-namespace:MyControls20210528  "
    
    
      <MyTxtb:MyTextBlock Text="下一个" Command="{Binding CMD_ClickTheLabel}"/>
    

    在Command绑定正确的情况下,鼠标左键能够触发
    参考: https://docs.microsoft.com/zh-cn/dotnet/desktop/wpf/advanced/how-to-implement-icommandsource?redirectedfrom=MSDN&view=netframeworkdesktop-4.8

    没有标记转载的情况下,如果有更好的优化麻烦电邮1808937496@qq.com或者留言答复,Thank You
  • 相关阅读:
    SpringMVC文件下载
    Servlet3.0文件上传
    SpringMVC拦截器的使用(入门)
    SpringMVC文件上传
    SpringMVC后台数据校验
    SpringMVC@InitBinder使用方法
    C++ this指针
    C++ 析构函数
    C++ 构造函数
    C++ 成员函数的实现
  • 原文地址:https://www.cnblogs.com/wandia/p/14821583.html
Copyright © 2011-2022 走看看