• 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
  • 相关阅读:
    2.谈谈算法
    1.数据结构和算法笔记
    初次使用博客
    Unity中关于在一个场景中使用多个摄像机
    基于unity的单例设计模式写法
    unity3D读取Txt文件中信息
    转载雨松的unity中使用ITween插件和ITweenPath
    Unity3D游戏开发之数据持久化PlayerPrefs的使用
    [转载]Unity3d更改3d Text的字体的材质球的shader,使字体不显示
    C#写的Socket Server端在unity运行时和关闭时没事,但是在打开直接unity崩溃问题
  • 原文地址:https://www.cnblogs.com/wandia/p/14821583.html
Copyright © 2020-2023  润新知