【发布时间】:2020-03-03 19:10:09
【问题描述】:
我正在努力实现以下目标:
这些项目基于 Prism MVVM 环境。在我的主要 ResourceDictionary 中,我有一个适用于所有自定义 MyDataGrid 的通用 ContextMenu。 此上下文菜单绑定到当前 MyDataGrid 的 ViewModel,其中声明了一些命令。这部分已经到位并按预期工作。
现在我还需要禁用一些基于一些自定义 MyDataGrid 依赖属性的 ContextMenu 函数。
我面临的问题是,我只能到达已使用 PlacementTarget 单击的单个 DataGridRow,但不能到达声明依赖属性的父 MyDataGrid。
这是 ContextMenu,它绑定到 DataGridRow:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyPrismModule.Wpf">
<ContextMenu x:Key="MyCommonContextMenu">
<MenuItem Header="MyFunction"
Command="{Binding
Path=PlacementTarget.Tag.Template.MyFunction,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
CommandParameter="{Binding
Path=PlacementTarget,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
<MenuItem.Style>
<Style TargetType="MenuItem" BasedOn="{StaticResource MaterialDesignMenuItem}">
<Setter Property="IsEnabled" Value="False"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Parent.MyDependencyProperty }" Value="True">
<Setter Property="IsEnabled" Value="True"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
</ContextMenu>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyDataGrid}}}"/>
<Setter Property="ContextMenu" Value="{Binding Source={StaticResource MyCommonContextMenu}}"/>
</Style>
为了达到 MyDataGrid.MyDependencyProperty 我尝试了这个 PlacementTarget.Parent.MyDependencyProperty 但它不起作用。
使用 Parent 的想法最初取自这里:https://social.msdn.microsoft.com/Forums/vstudio/en-US/1b01ba4e-d6a8-4e95-b8a0-bd3633bca2bd/binding-to-another-element-inside-a-template?forum=wpf
感谢所有愿意花时间研究此事的人。
【问题讨论】: