[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 ... 
随机推荐
- 张高兴的 Windows 10 IoT 开发笔记:0.96 寸 I2C OLED
			This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub:https://github.co ... 
- 【转】Json判断是否存在某个属性和遍历各个属性和值
			var field='uid'; var jsonObj={uid:'001'}; 一. jsonObj[field] != undefined //注意:如果field值正好是undefined那就 ... 
- bootstrap-multiselect样式修改
			问题 bootstrap-multiselect是一款相当不错的bootstrap风格下拉框组件,但是它的某些样式我不是很喜欢,按钮文本和下拉符号 “” 都是居中的,且下拉列表的宽度也没有跟随变动. ... 
- Extjs tree 过滤查询功能
			转载: http://blog.csdn.net/xiaobai51509660/article/details/36011899 Extjs4.2中,对于treeStore中未实现filterBy函 ... 
- (转)获取安卓iOS上的微信聊天记录、通过Metasploit控制安卓
			在这篇文章中我们将讨论如何获取安卓.苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制.文章比较基础.可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对 ... 
- 访问 IIS 元数据库失败解决问题的方法
			近日调试一Asp.net程序,出现了“访问 IIS 元数据库失败”的错误信息,最后经过搜索发现了解决问题的方法. 解决方法如下: 1.依次点击“开始”-“运行”. 2.在“运行”栏内输入 “C:\WI ... 
- PAT乙级1005
			1005 继续(3n+1)猜想 (25 分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程 ... 
- HDU 1257 最少拦截系统(贪心 or LIS)
			题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) ... 
- K2使用Nginx做负载均衡
			K2使用Nginx做负载均衡 K2目前是支持Load Balancing这种方式,来做负载均衡,也可以使用F5来做负载均衡,但这次我使用nginx来实现K2的负载均衡 下载nginx 请下载nginx ... 
- #leetcode刷题之路38-报数
			报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下:1. 12. 113. 214. 12115. 1112211 被读作 "one 1" (&quo ... 
