直接上代码,首先是一个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的问题的更多相关文章

  1. WPF ListBoxItem模板中添加CheckBox选中问题

    原文:WPF ListBoxItem模板中添加CheckBox选中问题 是这样的,需要一个ListBox来展示照片,并添加一个选中的CheckBox.这就需要对ListBox的ItemTemplate ...

  2. WPF中ContextMenu通过CommandParameter传参

    场景:ListBox中有个ContextMenu,希望点击其中一个菜单项的时候把ListBox当做CommandParameter传递给Command,但是发现无论是通过ElementName还是Re ...

  3. wpf DATAgrid模板中button 命令绑定以及命令参数绑定

    场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...

  4. wpf/Silverlight/wp中如何绑定模板中的属性

    <Style TargetType="{x:Type TabItem}" x:Key="EditorTabItemStyle"> <Sette ...

  5. WPF数据模板中绑定事件不触发问题

    今天比较闲,做一个练手的项目,结果在xaml中写了一个用户的数据模板后,在其中的某个Canvas上绑定了一个鼠标左击的事件,结果调试的时候,无论怎么点击都不跳到断点那里,百思不得其解. 之后尝试不绑定 ...

  6. wpf mvvm datagrid 中button绑定命令方法

    <DataGridTemplateColumn Header="设备状态" IsReadOnly="True" Width="150" ...

  7. 解析angularjs中的绑定策略

    一.首先回顾一下有哪些绑定策略? 看这个实在是有点抽象了,我们来看具体的实例分析吧! 二.简单的Demo实例 @绑定:传递一个字符串作为属性的值.比如 str : ‘@string’ 控制器中代码部分 ...

  8. WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法

    原文:WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法 问题描述 今天发现如果我想在一个TextBlock弄一个右键菜单,并且使用Command绑定,结果发 ...

  9. WPF Binding ElementName方式无效的解决方法--x:Reference绑定

    原文:WPF Binding ElementName方式无效的解决方法--x:Reference绑定 需求: 背景:Grid的有一个TextBlock name:T1和一个ListBox,ListBo ...

随机推荐

  1. * args 和 **kwargs

    def func(*args, **kwargs): print(args,kwargs) func("对", "哦",o=4, k=0) 结果---> ...

  2. python3编程的一些实用技巧1

    1.choice函数:返回一个列表,元组,字符串的随机项   :   调用时应导入random模块,如from random import choice 2.print 两个字符串, 逗号,+号进行连 ...

  3. MySQL管理.md

    用户管理 创建 举例 mysql> create user test@localhost identified by 'password'; Query OK, 0 rows affected ...

  4. 1.5 Community and Conferences(社区和讨论组)+ 私货

    1.5 Community and Conferences(社区和讨论组)+ 私货 下面是一些和科学计算,数据处理相关的Python社群和讨论组,如果有什么问题可以进行提问: pydata: A Go ...

  5. linux增加自己的可执行目录 $PATH

    Linux中有三种方法可以添加用户的路径到系统路径PATH, 以添加 /etc/apache/bin 为例: 方法一: 直接在命令行中输入: #PATH=$PATH:/etc/apache/bin 这 ...

  6. muduo 网络库学习之路(一)

    前提介绍: 本人是一名大三学生,主要使用C++开发,兴趣是高性能的服务器方面. 网络开发离不开网络库,所以今天开始学一个新的网络库,陈老师的muduo库 我参考的书籍就是陈老师自己关于muduo而编著 ...

  7. 用HTML编写淘宝页面

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  8. hadoop环境搭建-伪分布模式

    Appache hadoop 版本:2.77   jdk:1.8  系统:centos7 注意不要在root下解压,要单独建一个用户安装hadoop及其组件. 一.先查看系统是否有自带j #dk: r ...

  9. springboot快速入门(四)——数据库操作

    一.Spring-Data-Jpa的引入 先给出官方文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ IBM ...

  10. vb6 使用msxml2.serverxmlhttp 请求HTTP

    Private Sub Command1_Click() Command1.Enabled = False GetHtmlStr Text1 End Sub Private Sub GetHtmlSt ...