最近在项目中自己写了一个控件A,继承自contentcontrol,实现了icommandsource接口。(因需求特殊并没有使用buttonbase及它的派生类为基类),控件A在测试程序中运转良好,绑定的命令响应方法能够被正确执行。下边代码是控件A执行命令的部分:

RoutedEventArgs rea = new RoutedEventArgs(Button.ClickEvent, this);
RaiseEvent(rea); if (!rea.Handled && Command != null)
{
Command.Execute(CommandParameter);
}

  

但在实现项目中,我将控件A放在了另一个较为复制杂的控件B的template中,并在控件B的commandbindings中设置了命令绑定,命令使用的是WPF原生命令类型RoutedUICommand。这个时候诡异的事件发生了,不论我怎么在template里边设置控件A的commandtarget属性,上边代码执行了Command.Execute(CommandParameter)这句之后,B绑定的命令响应方法都不会被调用。

之后在群里问了一些大神,都没有得到解决方法,于是乎查了下.net码源,看看buttonbase是如何处理这个问题。

不看不知道,一看吓一跳,在buttonbase中处理命令时,用到了大量的.net项目里的internal类或方法(微软这是成心留一手啊!)

#from buttonbase.cs        
/// <summary>
/// This virtual method is called when button is clicked and it raises the Click event
/// </summary>
protected virtual void OnClick()
{
RoutedEventArgs newEvent = new RoutedEventArgs(ButtonBase.ClickEvent, this);
RaiseEvent(newEvent); MS.Internal.Commands.CommandHelpers.ExecuteCommandSource(this);
}

  #from CommandHelpers.cs

internal static void ExecuteCommandSource(ICommandSource commandSource)
{
CriticalExecuteCommandSource(commandSource, false);
} internal static void CriticalExecuteCommandSource(ICommandSource commandSource, bool userInitiated)
{
ICommand command = commandSource.Command;
if (command != null)
{
object parameter = commandSource.CommandParameter;
IInputElement target = commandSource.CommandTarget; RoutedCommand routed = command as RoutedCommand;
if (routed != null)
{
if (target == null)
{
target = commandSource as IInputElement;
}
if (routed.CanExecute(parameter, target))
{
routed.ExecuteCore(parameter, target, userInitiated);
}
}
else if (command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}

 从上边.net这几段代码里我们可以看到,buttonbase在执行命令里的时候,使用的是 CommandHelpers这个内部类中定义的方法,而这个方法的核心逻辑是先判断使用的命令是不是RoutedCommand类型,如果是的话则可能会使用RoutedCommand的内方法ExecuteCore去执行命令,而在这个方法中才有commandtarget这个参数。

总结一下,如果你的控件不是继承自buttonbase,那么就不能使用RoutedCommand.RoutedCommand这个内部方法,不能使用这个内部方法那么RoutedCommand就不会认你自己设置的commandtarget,那么如果.net库自己找到target不对,你再怎么设置也是徒劳。。。(我感觉被微软深深伤害了)

说明白点,不要把自己实现icommandsource的类型与routedcommand搭配使用!

WPF之坑——ICommandSource与RoutedUICommand的更多相关文章

  1. 我遇到的WPF的坑

    转自 林德熙Blog 本文:我遇到的WPF的坑 目录 单例应用在多实例用户无法使用 标记方法被使用 当鼠标滑过一个被禁用的元素时,让ToolTip 显示 获取设备屏幕数量 获取当前域用户 绑定资源文件 ...

  2. WPF之坑——surface触控失灵之谜

    本次又遇到了WPF编写触控程序的一个问题,虽然已解决,但原因确搞不太明白,希望有大神看到这篇文章帮我解答. 在项目中实现了自己定义的icommandsource,因为需要对触控有特殊需求,控件对鼠标与 ...

  3. WPF C# 命令的运行机制

    1.概述 1.1 WPF C# 命令的本质 命令是 WPF 中的输入机制,它提供的输入处理比设备输入具有更高的语义级别. 例如,在许多应用程序中都能找到的“复制”.“剪切”和“粘贴”操作就是命令. W ...

  4. wpf图片定点缩放

    去年犯小人,万事不顺,4月刚换工作,开始新工作 遇到一个小问题,需要读取图片,然后对图片进行定点缩放,很简答的逻辑,很简单的代码,但是,这尼玛我被wpf给坑了,这一坑就是三天 好了,很简单的一个UI ...

  5. 【Beta版本】冲刺-Day7

    队伍:606notconnected 会议时间:12月15日 目录 一.行与思 二.站立式会议图片 三.燃尽图 四.代码Check-in 一.行与思 张斯巍(433) 今日进展:修改界面,应用图标 明 ...

  6. 客户端浏览器- UWP兼容版本WebView

    WebView简介 在win10之前,浏览器控件有WPF版本webBrowser.Winform版本WebBrowser,浏览器内核为IE. win10之后,微软不再维护原有的WebBrowser,转 ...

  7. 2019-8-28-WPF-开发

    title author date CreateTime categories WPF 开发 lindexi 2019-8-28 11:3:39 +0800 2018-2-13 17:23:3 +08 ...

  8. 2019-10-16-WPF-控件-Content-的内容不显示下划线字符串

    title author date CreateTime categories WPF 控件 Content 的内容不显示下划线字符串 lindexi 2019-10-16 09:21:32 +080 ...

  9. dx wpf的各种坑

    这篇随笔总结dx wpf使用中的各种坑,持续更新~ LookUpEdit里内嵌的DXGrid的名字必须是"PART_GridControl",不能不写.也不能写错.我对比了2个小时 ...

随机推荐

  1. 对象之int介绍

    #Auther Bob #--*--conding:utf-8 --*-- #创建两个int的对象,age1和age2 age1 = 10 age2 = int(1) #查看对象的类 print(ty ...

  2. 单词拆分 I · Word Break

    [抄题]: 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. s = "lintcode" dict = ["lint" ...

  3. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  4. 12-matlab简单读excel

    数据读入: clc; clear; AllNeedDate = xlsread('E:\a-建模\2018-5月校赛\2018年数学建模校内挑战赛题目\挑战赛A题\附件2:各城镇月度需求数据.xlsx ...

  5. linux 下 php 安装 ZeroMQ 扩展

    一.下载安装源码包 ZeroMQ源码包下载地址: http://zeromq.org/area:download 如:zeromq-4.1.4.tar.gz   php的zmq扩展源码包 https: ...

  6. Oracle增加一列、修改一列数据类型

    Oracle增加一列.修改一列数据类型: 添加一列: alter   table   A   add( CFYJSNR  varchar2(20)); 修改列: alter  table A  ren ...

  7. 用Vue-cli生成vue+webpack的项目模板怎么设置为vue1.0版本?

    用Vue-cli生成vue+webpack的项目模板 $ npm install -g vue-cli $ vue init webpack my-project $ cd my-project $ ...

  8. 1. Install Git and GitExtension

    Install Git Step 1:   Run

  9. linux 操作系统rz sz 快速上传和下载文件

    ## Centos  安装  rz  sz yum install lrzsz ### Ubuntu  安装 apt-get install lrzsz

  10. Javascript 常用扩展方法

    这篇文章纯粹是为了保存这些方法,供以后翻阅,其实一直保存在 evernote 里面,但觉得还是放到对的地方会好点. 现在收录的很少,希望以后会慢慢增多. 数组扩展 contains,remove 扩展 ...