How to bind a Command on a ContextMenu within a DataTemplate using MVVM
Since the Popuup control has it's separate visual tree, you cannot use find ancestor to find the Grid. The trick here is to use the PlacementTarget property, that contains the element, the ContextMenu is aligned to, what is the Grid in our case.
But this is only half of the solution. Because of the data template, the DataContext is set to a dataitem, and not the view model. So you need another relative source lookup, to find the view model. Trick Nr. 2 is to use the Tag property to bind the view model from outside to the grid, which is the PlacementTargetused above. And there we are.
<DataTemplate>
<Grid Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}">
<Grid.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Content="Cut" Command="{Binding CutCommand}" />
<MenuItem Content="Copy" Command="{Binding CopyCommand}" />
<MenuItem Content="Paste" Command="{Binding PasteCommand}" />
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataTemplate>
How to bind a Command on a ContextMenu within a DataTemplate using MVVM的更多相关文章
- 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令
[源码下载] 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 作者:webabcd ...
- 背水一战 Windows 10 (23) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令
[源码下载] 背水一战 Windows 10 (23) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令 作者:webabcd ...
- MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令
介绍背水一战 Windows 10 之 MVVM(Model-View-ViewModel) 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令 ...
- MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令
介绍背水一战 Windows 10 之 MVVM(Model-View-ViewModel) 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 ...
- 使用command对象操作数据库
1.Command对象查询数据库 protected void Button1_Click(object sender, EventArgs e) { //读取web.config节点配置 strin ...
- WPF Command CanExecute 触发一次的问题
昨天在项目中遇到一个问题,按钮bind了Command后,利用CanExecute控制它的是否可点击.结果却在初始化viewmodel的时候执行了一次CanExecute,之后一直不触发,按钮的可用性 ...
- [WPF]解决模板中ContextMenu绑定CommandParameter的问题
直接上代码,首先是一个ContextMenu的模板: <ContextMenu x:Key="Menu" BorderThickness="0.3" Fo ...
- WPF之ContextMenu的命定绑定
在WPF中右击菜单项的XMAL代码是: <ContextMenu x:Key="sampleContextMenu"> <MenuItem Header=&quo ...
- [转]WPF命令集 Command
在我们日常的应用程序操作中,经常要处理各种各样的命令和进行相关的事件处理,比如需要复制.粘贴文本框中的内容;上网查看网页时,可能需要返回上一网页查看相应内容;而当我们播放视频和多媒体时,我们可能要调节 ...
随机推荐
- Mac Sublime Text 3
安装Package Control安装过程: 使用快捷键 control + ` 或者菜单栏选择View > Show Console安装Package Control参考官方页面.Sublim ...
- shouldRasterize 光栅化、(缓存)复用、内存、内容稳定
使用场景:稳定的视图(layer)被反复使用或进行动画: 本质:牺牲内存解放cpu: 反例:牺牲内存牺牲cpu. 单次使用或者视图有变动,shouldRasterize不会有任何用途,反而会牺牲内存. ...
- geth --rpcaddr
当想要实现从另一台电脑连接本电脑上开启的geth客户端时,需要将--rpcaddr设置为本电脑的ip地址,如下: geth --datadir data0 --networkid --port --r ...
- jquery全选 反选
//全选 反选 $('#chkAll').on('click',function(){ $('input.chkbox').prop('checked',$(this).prop('checked') ...
- PAT——1061. 判断题
判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...
- Mac 下 SVN 的使用
在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...
- 查看mysql中所有表的数据记录
select table_name,table_rows from tables where TABLE_SCHEMA = 'database name' order by table_rows de ...
- 理解numpy exp函数
exp,高等数学里以自然常数e为底的指数函数 Exp:返回e的n次方,e是一个常数为2.71828 Exp 函数 返回 e(自然对数的底)的幂次方. a = 1 print np.exp(a) a ...
- Java中如何判断一个字符串是否为数字
方法一:异常处理 public static boolean isInteger(String str){ try { Integer i = Integer.parseInt(str); retur ...
- 轻量ORM-SqlRepoEx (十六)最佳实践之Dapper(2)
简介:SqlRepoEx是 .Net平台下兼容.NET Standard 2.0人一个轻型的ORM.解决了Lambda转Sql语句这一难题,SqlRepoEx使用的是Lambda表达式,所以,对c#程 ...