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
在我们日常的应用程序操作中,经常要处理各种各样的命令和进行相关的事件处理,比如需要复制.粘贴文本框中的内容;上网查看网页时,可能需要返回上一网页查看相应内容;而当我们播放视频和多媒体时,我们可能要调节 ...
随机推荐
- Debian 静态网络配置
allow-hotplug enp6s0 iface enp6s0 inet static address gateway 192.168.2.1 # dns-* options are implem ...
- linux(ubuntu)因为代理网页无法访问
当有一天你打开浏览器的时候,页面上显示,未链接到网络,请检查代理服务器,出现了如下图所示的情况: 但是你检查一下了网络,可以ping通,而且在同一个网络下,你的手机平板可以上网,那么你就想一想,昨天i ...
- Redis.md
rpm 包安装 CentOS 系列系统安装redis可以通过第三方提供的rpm包进行安装: # yum install -y epel-release # yum install -y redis 源 ...
- Discuz!在线中文分词服务
Discuz!在线中文分词服务是基于API返回分词结果的.在项目中,我们只需要一个函数即可方便地进行分词.关键词提取.以下是根据Discuz!在线分词服务API写的函数,测试可正常运行: 代码代码如下 ...
- 【LGP2045】方格取数加强版
题目 还纠结了一下是费用流还是最小割 最终还是决定让最小割去死吧 我们的问题就是让一个点的点权只被计算一次 考虑拆点 将所有点拆成入点和出点,入点向出点连流量为\(1\)的边 每一个出点往下连能到达的 ...
- DOM操作怎样添加、移除、移动、复制。创建和查找节点?
(1)创建新节点 createDocumentFragment() //创建一个DOM片段 createElement() //创建一个具体的元素 createTextNode() //创建一个文本节 ...
- Kali-linux其他信息收集手段
上面介绍了使用不同的工具以操作步骤的形式进行了信息收集.在Kali中还可以使用一些常规的或非常规方法来收集信息,如使用Recon-NG框架.Netdiscover工具和Shodan工具等.本节将介绍使 ...
- 发送邮件 html格式
下面黄色代码为实现发送邮件 FileStream fs = new FileStream(Server.MapPath("../ImportUserIOExcel/productConsul ...
- xgboost和gbdt区别
1. xgboost在目标函数中加入了正则化项,当正则化项为0时与传统的GDBT的目标函数相同2. xgboost在迭代优化的时候使用了目标函数的泰勒展开的二阶近似,paper中说能加快优化的过程!! ...
- HDU 1224 Free DIY Tour(spfa求最长路+路径输出)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...