问题:在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. exp、Exploit、Exploit Pack、exp-gui、Payload、MetaSploit都是啥?

    对于走在安全路上的小菜来说,这几个exp.Exploit.Exploit Pack.exp-gui.Payload.MetaSploit名词着实把人转的不轻,以下给大家解释下: Exp,就是Explo ...

  2. iOS自己主动化測试的那些干货

    前言 假设有測试大佬发现内容不正确.欢迎指正,我会及时改动. 大多数的iOS App(没有持续集成)迭代流程是这种 也就是说.測试是公布之前的最后一道关卡.假设bug不能在測试中发现,那么bug 就会 ...

  3. linux(centos6)搭建ftp服务器 -摘自网络

    前提 ssh服务已经开启,关闭防火墙,主机和虚拟机能ping通 查看ssh和防火墙的状态 service sshd status service iptables status 开启ssh服务 ser ...

  4. Oracle connect by 层级结构查询

    公司组织架构表:organization 一.查询子级 select * from organizationstart with id='1'connect by prior id = parent_ ...

  5. jQuery学习笔记(jquery.ui插件)

    官网地址:http://ui.jquery.com/ jQuery UI源自于一jQuery插件-Interface.目前版本是1.10.3,需要jQuery 1.6以上版本支持. jQuery UI ...

  6. [Windows Azure] Configuring and Deploying the Windows Azure Email Service application - 2 of 5

    Configuring and Deploying the Windows Azure Email Service application - 2 of 5 This is the second tu ...

  7. 聊聊javascript的null和undefined

    只要是说到js的变量和数据类型,就脱不开null和undefined,这兄弟俩就是js的重要基础,不可不察,无数的同学们都用过放大镜多角度多批次地研究过这兄弟俩,真是深受欢迎.^-^ js也真是怪异, ...

  8. Standard C 之 math.h和float.h

    对于C Standard Library 可以参考:http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ 或者 http://www.cplusplus.c ...

  9. 李洪强iOS经典面试题

    李洪强iOS经典面试题 1. struct和class的区别 swift中,class是引用类型,struct是值类型.值类型在传递和赋值时将进行复制,而引用类型则只会使用引用对象的一个"指 ...

  10. (转)在Docker中运行Java:为了防止失败,你需要知道这些

    转自:https://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=2649693848&idx=1&sn=4e9ef7e2a9d ...