WPF DataTomplate中Command无效
问题:在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无效的更多相关文章
- WPF/Silverlight中的RichTextBox总结
WPF/Silverlight中的RichTextBox总结 在WPF或者是在Silverlight中有个非常强大的可以编辑的容器控件RichTextBox,有的时间会采取该控件来作为编辑控件.鉴 ...
- 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 ...
- 浅谈WPF本质中的数据和行为
WPF缩写为Windows Presentation Foundation的缩写,本文所要谈的就是WPF本质中的数据和行为,希望通过本文能对大家了解WPF本质有所帮助. 如果自己来做一个UI框架,我们 ...
- WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍
WPF自学入门(十一)WPF MVVM模式Command命令 在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...
- [转]WPF命令集 Command
在我们日常的应用程序操作中,经常要处理各种各样的命令和进行相关的事件处理,比如需要复制.粘贴文本框中的内容;上网查看网页时,可能需要返回上一网页查看相应内容;而当我们播放视频和多媒体时,我们可能要调节 ...
- WPF Binding ElementName方式无效的解决方法--x:Reference绑定
原文:WPF Binding ElementName方式无效的解决方法--x:Reference绑定 需求: 背景:Grid的有一个TextBlock name:T1和一个ListBox,ListBo ...
- 在WPF程序中使用摄像头兼谈如何使用AForge.NET控件(转)
前言: AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架,它包括图像处理.神经网络.遗传算法和机器学习等.在C#程序中使用摄像头,我习惯性使用AForge.NET提供的类库.本 ...
- WPF 程序中启动和关闭外部.exe程序
当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...
- Angular 动态生成html中 ng-click无效
bodyApp.controller('customersCtrl', function ($scope, $http, cfpLoadingBar,$compile) { $scope.test = ...
随机推荐
- eclipse导入class文件
右键src文件夹->build path->config build path->library->add class folder->create new folder ...
- 【转】Currying 的局限性
Currying 的局限性 很多基于 lambda calculus 的程序语言,比如 ML 和 Haskell,都习惯用一种叫做 currying 的手法来表示函数.比如,如果你在 Haskell ...
- Windows键盘消息处理
原文链接: http://blog.sina.com.cn/s/blog_5f8817250100taab.html 本文大部分来自MSDN和网友的博客,我在实践的基础上再作了一些总结. 1,虚拟键( ...
- Oracle in和exists效率问题分析
--------------------------in和exists效率问题------------- 单说in和exsist,in的效率较差.关于EXISTS与IN的区别:EXISTS检查是否有结 ...
- [转]@Transactional spring 配置事务 注意事项
@Transactional spring 配置事务 注意事项 [@more@] @Transactional spring 配置事务 注意事项 1. 在需要事务管理的地方加@Transactiona ...
- 安装 Vbundle 的笔记
Vbundle 挺好用的,能够很方便管理Vim的一些插件.虽然Vbundle的安装方法看的很简单,但是它的配置却让我弄了很久,现在记录如下,方便后面安装时再出现相同的问题: 我按照这里的官方提示的安装 ...
- Web Service 或 WCF调用时读取 XML 数据时,超出最大字符串内容长度配额(8192)解决方法
1.调用服务时服务 当我们使用 Web Service 或 WCF 服务时,常把读取的数据转化为string类型(xml格式),当数据量达到一 定数量时,会出现以下异常: 错误:格式化程序尝试对消息反 ...
- Invalid configuation file. File "**********" was created by a VMware product with more feature than this version of VMware Workstation and cannot be
大概就是说你的之前用来创建虚拟机的VM版本太高,被移植的VM版本太低.所以你需要改一点东西. 打开你的虚拟机的目录(不是VM的),然后看到你很多文件. 然后你看到*.vmx的文件(实在找不到就按文件类 ...
- Windows: 打开关闭网络连接的方法
在Cmd中键入 netsh interface set interface name="本地连接" admin=disablednetsh interface set interf ...
- how to build jdk 9 source code
http://hg.openjdk.java.net/build-infra/jdk9/raw-file/tip/README-builds.html#vs2013 http://royvanrijn ...