问题:在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. Android逆向之旅---破解&quot;穿靴子的猫&quot;游戏的收费功能

    一.游戏收费分析 游戏收费非常正常的,可是玩游戏最恶心的就是你还没玩就要充值,非常恼火,事实上我不怎么玩游戏,主要是给小孩子们弄,比方如今好多小屁孩们喜欢玩水果忍者这个游戏.可是这个游戏在没有開始玩的 ...

  2. CListCtrl行高问题最终解决方法

    原文链接: http://blog.csdn.net/benny5609/article/details/1967078 解决方案: 1. 设置List Control的属性 Owen Draw Fi ...

  3. RDLC 设置表的重复标题行(在每页中显示标题行)

    在Tablix 属性对话框中勾选“在每一页上重复标题行”及“滚动时保持标题可见”结果没有用. 如果您使用的是一个“表”式布局,有一个简单的方法,可以尝试以下四个步骤: 1.在分组窗格中,单击窗格上的小 ...

  4. 【转载】使用 Google Guava 美化你的 Java 代码

    转载地址: https://blog.csdn.net/wisgood/article/details/13297535 原文地址:https://my.oschina.net/leejun2005/ ...

  5. glide 镜像

    运行glide install 失败  国内墙的原因, 某些网站上不去 [ERROR]Update failed for golang.org/x/crypto: Cannot detect VCS ...

  6. logtash 接收多配置文件

    [root@10-19-148-59 etc]# vim front_esb.conf input { beats { type => beats port => 5077 } } fil ...

  7. Visual F# Power Tools 简单介绍

    Visual F# Power Tools 简单介绍 Auto-generating XmlDoc 当在函数定义的前面输入 ///< 以后.会自己主动生成 XML 文档.并会自己主动提取函数中的 ...

  8. [na]数据链路层&网络层协议小结截图版

    ip层:分组选路 tcp:端到端的通信 中间系统没必要有应用程序,分组选路即可 应用程序中隐藏所有的物理细节. 语音肯定用udp linux主版本 次版本号 修订号 次版本为偶数说明是稳定版.奇数是开 ...

  9. HTML杂记

    1.URL  uniform resource locator 遵循格式: scheme://host.domain:port/path/filename scheme - 定义因特网服务的类型.最常 ...

  10. 【小白的CFD之旅】小结及预告

    这是小白系列的索引,后续会继续更新. 已更新的部分 01 引子02 江小白03 老蓝04 任务05 补充基础06 流体力学基础07 CFD常识08 CFD速成之道09 初识FLUENT10 敲门实例1 ...