[WPF]解决模板中ContextMenu绑定CommandParameter的问题
直接上代码,首先是一个ContextMenu的模板:
<ContextMenu x:Key="Menu" BorderThickness="0.3" FontSize="13" DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="复制" Command="{Binding Source={x:Static vm:ViewModelLocator.Instance},Path=MainWindowVM.CopyImageCommand}"
CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" />
<MenuItem Header="另存为..." Command="{Binding Source={x:Static vm:ViewModelLocator.Instance},Path=MainWindowVM.SaveImageCommand}"
CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" />
</ContextMenu>
然后是DataTemplate的内容:
<DataTemplate x:Key="CustomerImageDataTemplate">
<Button Command="{Binding DataContext.ShowImageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Cursor="Hand"
CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=StackPanel}}" Padding="3">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<Border Name="imgBorder" BorderBrush="{StaticResource ListBorder}" CornerRadius="3" BorderThickness="0"
Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" Margin="0">
</Border>
<Image x:Name="ImageMsg" Source="{Binding TextContent}" Stretch="Uniform" Width="120" Height="80"
ContextMenu="{StaticResource Menu}" Tag="{Binding}">
<Image.OpacityMask>
<VisualBrush Visual="{Binding ElementName=imgBorder, Mode=OneTime}" />
</Image.OpacityMask>
</Image>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
上面的Button还展示了将图片四角设置为圆角的方法。
补充后来修改过的ContextMenu和模板:
<ContextMenu x:Key="Menu" BorderThickness="0.3" FontSize="13" DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem x:Name="MntCopy" Header="复制" Command="{Binding Source={x:Static vm:ViewModelLocator.MainWindowVM},Path=CopyImageCommand}" FlowDirection="LeftToRight"
CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" />
<MenuItem x:Name="MntSaveAs" Header="另存为..." Command="{Binding Source={x:Static vm:ViewModelLocator.MainWindowVM},Path=SaveImageCommand}" FlowDirection="LeftToRight"
CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" />
<MenuItem Header="撤回" Command="{Binding Source={x:Static vm:ViewModelLocator.MainWindowVM},Path=WithdrawCommand}" FlowDirection="LeftToRight"
CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}"
CommandParameter="{Binding PlacementTarget.DataContext.MsgID, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
<Button Command="{Binding Source={x:Static vm:ViewModelLocator.MainWindowVM}, Path=ShowImageCommand}" Cursor="Hand" Margin="5,0,0,0"
CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=StackPanel}}" Padding="3">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<Border Name="imgBorder" BorderBrush="{StaticResource ListBorder}" CornerRadius="0" BorderThickness="0"
Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
ContextMenu="{StaticResource Menu}" Tag="{Binding}">
</Border>
<Image x:Name="ImageMsg" Source="{Binding TextContent}" Stretch="Uniform" MinWidth="20" MinHeight="50" MaxWidth="150" MaxHeight="150"
ContextMenu="{StaticResource Menu}" Tag="{Binding}">
<Image.OpacityMask>
<VisualBrush Visual="{Binding ElementName=imgBorder, Mode=OneTime}" />
</Image.OpacityMask>
</Image>
</Grid>
</ControlTemplate>
</Button.Template>
<Button.InputBindings>
<MouseBinding MouseAction="RightClick" Command="{Binding Source={x:Static vm:ViewModelLocator.MainWindowVM}, Path=MsgContextMenuCommand}"
CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=StackPanel}}"/>
</Button.InputBindings>
</Button>
[WPF]解决模板中ContextMenu绑定CommandParameter的问题的更多相关文章
- WPF ListBoxItem模板中添加CheckBox选中问题
原文:WPF ListBoxItem模板中添加CheckBox选中问题 是这样的,需要一个ListBox来展示照片,并添加一个选中的CheckBox.这就需要对ListBox的ItemTemplate ...
- WPF中ContextMenu通过CommandParameter传参
场景:ListBox中有个ContextMenu,希望点击其中一个菜单项的时候把ListBox当做CommandParameter传递给Command,但是发现无论是通过ElementName还是Re ...
- wpf DATAgrid模板中button 命令绑定以及命令参数绑定
场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...
- wpf/Silverlight/wp中如何绑定模板中的属性
<Style TargetType="{x:Type TabItem}" x:Key="EditorTabItemStyle"> <Sette ...
- WPF数据模板中绑定事件不触发问题
今天比较闲,做一个练手的项目,结果在xaml中写了一个用户的数据模板后,在其中的某个Canvas上绑定了一个鼠标左击的事件,结果调试的时候,无论怎么点击都不跳到断点那里,百思不得其解. 之后尝试不绑定 ...
- wpf mvvm datagrid 中button绑定命令方法
<DataGridTemplateColumn Header="设备状态" IsReadOnly="True" Width="150" ...
- 解析angularjs中的绑定策略
一.首先回顾一下有哪些绑定策略? 看这个实在是有点抽象了,我们来看具体的实例分析吧! 二.简单的Demo实例 @绑定:传递一个字符串作为属性的值.比如 str : ‘@string’ 控制器中代码部分 ...
- WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法
原文:WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法 问题描述 今天发现如果我想在一个TextBlock弄一个右键菜单,并且使用Command绑定,结果发 ...
- WPF Binding ElementName方式无效的解决方法--x:Reference绑定
原文:WPF Binding ElementName方式无效的解决方法--x:Reference绑定 需求: 背景:Grid的有一个TextBlock name:T1和一个ListBox,ListBo ...
随机推荐
- NTRIP协议学习(一)
这篇博客讲得很清晰. https://blog.csdn.net/sinat_19447667/article/details/67637167 可以参考的文献包括:<多系统GNSS实时数据质 ...
- 面向对象程序设计_Task5_Calculator1.5.0
The 3rd part of the Calculator program _ FILE I/O 题目链接:第五次作业(计算器第三步) github链接:Calculator_1.5.0 第五次作业 ...
- php 实现八皇后问题
php实现的八皇后问题,可以推广到N皇后 <?php class Empress{ private $queen;//存储位置,例如$queen[2] = 3表示第三行的第四列位置,行列从0计数 ...
- 我的开源项目——Jerry
在日常工作中,经常会碰到一些问题,比如数字金额要写成千分位形式(1234 -> 123,4.00).要写成汉字大写形式(123 -> 壹佰贰拾叁圆),又比如要进行 cookie 读写操作, ...
- 有料面试题之--Object里面的方法
阿里的面试题里面有个题很奇妙:你知道Object类里面有哪些方法吗? 绝大部分猿类都知道 有hashcode .equals .clone.toString 只有部分人会回答有 wait和notify ...
- P2418 yyy loves OI IV
题目背景 某校2015届有两位OI神牛,yyy和c01. 题目描述 全校除他们以外的N名学生,每人都会膜拜他们中的某一个人.现在老师要给他们分宿舍了.但是,问题来了: 同一间宿舍里的人要么膜拜同一位大 ...
- React-Native 之 index.android.bundle
问题: index.android.bundle 这个bug 我相信很少同学会遇到,然而就是这个问题,困扰了我跟我的同事多天, 各种方法处理: 进入 android 目录 ./gradlew c ...
- 20155314 2016-2017-2 《Java程序设计》实验三 敏捷开发与XP实践
20155314 2016-2017-2 <Java程序设计>实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验知识点总结 (一)敏捷开发与XP 软件工程:把系统 ...
- mac下git安装和使用
1.下载git客户端,下载地址为:https://git-scm.com/download/mac 2.打开安装包,可以看到此时的界面为: 我们需要把.pkg的安装包安装到系统当中.我双击了安装包 ...
- FFMpeg笔记(五) 录制小视频时几个问题解决
1. YUV数据在使用avfilter scale时在特定的分辨率下UV分量不对 由于是小视频,那么分辨率不需要太高,但是有的视频源是1080p,甚至有的是4K的,所以对视频源进行scale非常有必要 ...