问题:在DataTomplate中添加一个Button,Button添加Command,但是Command生效。

原因:ItemTemplate的DataContext指代不明,需要改为父类的DataContext。

解决方法:使用RelativeSource关键字,手动指定DataComtext和Command。

<userControl:AutoHideTabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" Margin="5,0,0,0" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction
Command="{Binding Path=DataContext.TabItemClickCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=userControl:AutoHideTabControl}}"
CommandParameter="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}}"></i:InvokeCommandAction>
</i:EventTrigger></i:Interaction.Triggers>
</TextBlock>
<!-- CommandParameter, 将父类作为参数传给后台命令 -->
<Button Content="Test" Command="{Binding Path=DataContext.(viewModels:CaseViewModel.TabItemClickCommand), RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=userControl:AutoHideTabControl}}"CommandParameter="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}}"></i:InvokeCommandAction>>
</StackPanel>
</DataTemplate>
</userControl:AutoHideTabControl.ItemTemplate>

对应后台代码:

// ViewModel构造函数
public Constructors()
{
TabItemClickCommand = new DelegateCommand<object>(TabItemClick);
} //命令声明
public DelegateCommand<object> TabItemClickCommand{get; private set;} //命令实现
public void TabItemClick(object obj)
{
var control = obj as ContentPresenter;
}

参考:WPF ListBoxItem DataTempldate command 执行问题

WPF DataTomplate中Command无效的更多相关文章

  1. WPF/Silverlight中的RichTextBox总结

    WPF/Silverlight中的RichTextBox总结   在WPF或者是在Silverlight中有个非常强大的可以编辑的容器控件RichTextBox,有的时间会采取该控件来作为编辑控件.鉴 ...

  2. wpf自定义控件中使用自定义事件

    wpf自定义控件中使用自定义事件 1 创建自定义控件及自定义事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...

  3. 浅谈WPF本质中的数据和行为

    WPF缩写为Windows Presentation Foundation的缩写,本文所要谈的就是WPF本质中的数据和行为,希望通过本文能对大家了解WPF本质有所帮助. 如果自己来做一个UI框架,我们 ...

  4. WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍

    WPF自学入门(十一)WPF MVVM模式Command命令   在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...

  5. [转]WPF命令集 Command

    在我们日常的应用程序操作中,经常要处理各种各样的命令和进行相关的事件处理,比如需要复制.粘贴文本框中的内容;上网查看网页时,可能需要返回上一网页查看相应内容;而当我们播放视频和多媒体时,我们可能要调节 ...

  6. WPF Binding ElementName方式无效的解决方法--x:Reference绑定

    原文:WPF Binding ElementName方式无效的解决方法--x:Reference绑定 需求: 背景:Grid的有一个TextBlock name:T1和一个ListBox,ListBo ...

  7. 在WPF程序中使用摄像头兼谈如何使用AForge.NET控件(转)

    前言: AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架,它包括图像处理.神经网络.遗传算法和机器学习等.在C#程序中使用摄像头,我习惯性使用AForge.NET提供的类库.本 ...

  8. WPF 程序中启动和关闭外部.exe程序

    当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...

  9. Angular 动态生成html中 ng-click无效

    bodyApp.controller('customersCtrl', function ($scope, $http, cfpLoadingBar,$compile) { $scope.test = ...

随机推荐

  1. EasyUI 中GridView 满足某条件 改变行的背景色

    <table id='grid' class='easyui-datagrid' style='width:1500px;height:450px' url='Ajax-index.php?mo ...

  2. python的内置下载器

    python有个内置下载器,有时候在内部提供文件下载很好用. 进入提供下载的目录 # ls abc.aaa chpw.py finance.py lsdir.py ping.py u2d-partia ...

  3. C语言下的错误处理的问题

    下面是三种C语言的错误处理,你喜欢哪一种?还是都不喜欢? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /* 问题: 不充分,而且很容易出错,前 ...

  4. java 和 C++ Socket通信(java作为服务端server,C++作为客户端client,解决中文乱码问题GBK和UTF8)

    原文链接: http://www.cnblogs.com/kenkofox/archive/2010/04/25/1719649.html 代码: http://files.cnblogs.com/k ...

  5. html input控件总结

    Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍. 1,type=text 输入类型是text,这是我们见的 ...

  6. tips: javascript 参数传递含有空格怎么办?

    js 方法传参有时候会遇到空格,空格会报错,因为它会默认空格后是元素 解决方法就是使用 escape 和 unescape html: var title = escape(rowObject.tit ...

  7. Xilinx Vivado的使用详细介绍(2):综合、实现、管脚分配、时钟设置、烧写

    前面一篇介绍了从新建工程一直到编写代码进行行为仿真,这篇继续进行介绍. 修改器件型号 新建工程时选择过器件型号,如果新建好工程后需要修改型号,可以选择菜单Tools - Project Setting ...

  8. 关于chrome的开发调试方式

    chrome://inspect/#devices 调试移动设备app chrome://version 查看chrome浏览器版本信息 chrome://components/ 查看组件信息 上面的 ...

  9. 无法加载 DLL“ParkCOM.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E) 终结者

    C#调用利用C++写的dll 常遇到的情况是无法加载DLL"***.dll":找不到指定的模块(异常来自HRESULT:0x8007007E)终极解决方法如下: 1.产生原因 可能 ...

  10. [Windows Azure] Data Management and Business Analytics

    http://www.windowsazure.com/en-us/develop/net/fundamentals/cloud-storage/ Managing and analyzing dat ...