直接上代码,首先是一个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. ASP.NET Web Api vs Node.js Benchmark

    http://mikaelkoskinen.net/post/asp-net-web-api-vs-node-js-benchmark ASP.NET Web Api vs Node.js Bench ...

  2. Programming Assignment 2: Deques and Randomized Queues

    编程作业二 作业链接:Deques and Randomized Queues & Checklist 我的代码:Deque.java & RandomizedQueue.java & ...

  3. Python简单实现多级菜单

    # -*- coding: utf-8 -*- # @Time : 2018-06-01 13:40 # @Author : 超人 # @Email : huxiaojiu111@gmail.com ...

  4. Python的网络编程 Socket编程

    Socket是进程间通信的一种方式,与其他进程间通信的一个主要不同是:能实现不同主机间的进程间通信,网络上各种各样的服务大多都是基于Socket来完成通信的,要解决网络上两台主机间的通信问题,首先要唯 ...

  5. ueditor 百度编辑器 解决表格没有边框

    因为项目需要,发现直接从word和excel复制粘贴以后,居然在禅道上表格没有边框了,故查了一下 这里从word,以及excel粘贴复制,都能直接有边框了,同时在编辑器里面新增表格,也能直接显示边框了 ...

  6. Java并发编程--5.信号量和障碍器

    Semaphore信号量 简介 它本质上是一个共享锁,限制访问公共资源的线程数目,它也被称为计数信号量acquire()许可一个线程, Semaphore – 1; 没有可用的许可时,Semaphor ...

  7. Java实现Package编译和访问

    Java实现Package编译和访问 说明 所有文件都是使用UTF-8编码来写的,请不要用Windows记事本随便打开 Test.java文件中注释的方法说明了该类是不能访问其方法的 文件目录树 bi ...

  8. Jenkins+Github(Robotframework代码)

    个人记录,且为Windows10系统,仅供参考. 一. 准备 1. Github:安装git.注册github.将Robotframework代码更新到github. 2. Jenkins安装,并安装 ...

  9. Linux内存调试工具初探-MEMWATCH(转)

    C 语言作为 Linux 系统上标准的编程语言给予了我们对动态内存分配很大的控制权.这种自由可能会导致严重的内存管理问题,可能导致程序崩溃或随时间的推移导致性能降级. 内存泄漏(即 malloc()  ...

  10. 【css】box-shadow匹配ps投影效果/text-shadow

    参照资料:http://blog.csdn.net/lpandeng/article/details/72778451 box-shaow: 距离(x-offset y-offset)  虚影(blu ...