直接上代码,首先是一个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. laravel 资料

    1.http://maxoffsky.com/maxoffsky-blog/building-a-shop-with-laravel-tutorial-series-announcement/  一篇 ...

  2. 【理解】 Error 10053和 Error 10054

    1. 10053 这个错误码的意思是:  A established connection was aborted by the software in your host machine, 一个已建 ...

  3. js检测密码强度

    <script> function AuthPasswd(string) {     if(string.length >=6) {         if(/[a-zA-Z]+/.t ...

  4. LA5713 秦始皇修路 (mst)

    题意: 秦朝有n个城市,需要修路让每个城市都互相连通,现在可以免费修一条路,秦始皇希望他除了这条免费修的路外所需修的路的总和B最短,同时这条免费的路连接的人口之和A尽可能大,求最大的A/B是多少,城市 ...

  5. FFI (语言交互接口(Foreign Function Interface))

    FFI(Foreign Function Interface)是用来与其它语言交互的接口, 在有些语言里面称为语言绑定(language bindings), Java 里面一般称为 JNI(Java ...

  6. 时分秒倒计时的js实现

    示例: 剩余天数:08天02事02分02秒 html代码: <div id="timer" data-timer="20160628140203" sty ...

  7. [NOIp2009] $Hankson$の趣味题

    \(23333\)这是最近第二份在时间上吊打\(yjk\)的代码--啊哈哈哈哈哈哈哈 嗯,其实遇到这种单纯的\(gcd \ \ or \ \ lcm\)的题,我们都可以用一种比较简单的方法分析:唯一分 ...

  8. .Net实现表达式计算(公式) 表达式字符串

    文档原址:http://blog.csdn.net/fangxing80/article/details/5992661 实现复杂公式计算,比如含IF分支判断等,可考虑通过调用EXCEL公式获取值. ...

  9. oracle 事务 数据伪列

    在用户进行数据更新操作(DML)事务一定会起作用. 事务的出现会保证数据的完整性.一致性.在整个事务的处理过程之中主要使用两个操作命令: · 事务的提交(COMMIT):是真正的向数据库之中发出更新指 ...

  10. L2-022 重排链表(链表)

    题目: 给定一个单链表 L​1​​→L​2​​→⋯→L​n−1​​→L​n​​,请编写程序将链表重新排列为 L​n​​→L​1​​→L​n−1​​→L​2​​→⋯.例如:给定L为1→2→3→4→5→6 ...