【问题标题】:How to disable MenuItems based on a DataGrid DependencyProperty in a WPF ContextMenu如何基于 WPF ContextMenu 中的 DataGrid DependencyProperty 禁用 MenuItems
【发布时间】: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

感谢所有愿意花时间研究此事的人。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    其实你已经完成了所有的工作。小的改进是MyDataGrid 而不是MyDataGrid 设置为DataGridRow.Tag,稍后通过PlacementTarget.Tag 访问它。

    <ContextMenu x:Key="MyCommonContextMenu">
        <MenuItem Header="{Binding PlacementTarget.Tag.MyDependencyProperty, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
        <MenuItem Header="{Binding PlacementTarget.Tag.DataContext.YourViemodelProperty, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
    </ContextMenu>
    
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyDataGrid}}}"/>
        <Setter Property="ContextMenu" Value="{Binding Source={StaticResource MyCommonContextMenu}}"/>
    </Style>
    

    【讨论】:

    • 非常感谢 Rekshino 对我的启发!我很接近,但我看不到这个明显的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多