WPF MVVM 绑定可见性到动态 MenuItem

WPF MVVM Binding visibility to dynamic MenuItem

如何在命令 CanExecute* 方法更改其状态时更新上下文菜单项。

关于将 visibility 绑定到动态创建的 MenuItems(基于 ICommandsDataTemplates),我遇到了问题。 ContextMenu 是为 GridControl 创建的,它绑定了一些自定义参数。 我设法通过 Freezable 代理将这些参数绑定到 ContextMenu。 一切正常,除了 CanExecute* 不会改变 MenuItem 可见性。 如果 CanExecute* 有常量 e.CanExecute = true,那没关系(菜单项处于活动状态),但是当 CanExecute* 有一些逻辑并且可以有两种状态时,比 MenuItem 总是有 IsEnabled 设置为 false

一些代码:
ContextCommands 是 ICommand

的扩展
IEnumerable<ICommand<SomeClass>> ContextCommands

自定义菜单项

//CustomMenuItem is just extension of MenuItem  
public class CustomMenuItem : MenuItem

数据模板

<DataTemplate DataType="{x:Type....
<controls:CustomMenuItem Command="{Binding Path=WrappedCommand}" Visibility="{Binding Path=IsVisible, Converter={StaticResource VisibilityConverter}}">
<commandparameters ... (parameters works OK, so i skip that)>

网格声明

<controls:CustomGridControl Grid.Row="1" x:Name="MyGrid"
                                   ColumnDescriptions="{Binding Source.ColumnDescriptions}" 
                                   QueryableSource="{Binding Source.Query}"
                                   Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=Self}}"
                                    >
    <controls:CustomGridControl.Resources>
        <helpers:BindingProxyHelper x:Key="DataProxy" Data="{Binding ElementName=MyGrid, Path=., Mode=TwoWay}" />
    </controls:CustomGridControl.Resources>
    <controls:CustomGridControl.ContextMenu>
        <contextmenu:CustomContextMenu 
            DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"
            ItemsSource="{Binding ContextCommands}"
            BindableProperties="{Binding Data, Source={StaticResource DataProxy}, Mode=TwoWay}">
        </contextmenu:CustomContextMenu>
    </controls:CustomGridControl.ContextMenu>
</controls:CustomGridControl>

如何在CanExecute变化时调用动态MenuItem的Visibility? 我试图传递 DataContext 但没有效果,UI 没有改变 调试此绑定显示可见性已正确设置,但没有效果。

CAN EDIT FALSE
System.Windows.Data Warning: 56 : Created BindingExpression (hash=2852357) for Binding (hash=35737921)
System.Windows.Data Warning: 58 :   Path: 'IsVisible'
System.Windows.Data Warning: 60 : BindingExpression (hash=2852357): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=2852357): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=2852357): Attach to Controls.CustomMenuItem.Visibility (hash=36410781)
System.Windows.Data Warning: 67 : BindingExpression (hash=2852357): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=2852357): Found data context element: CustomMenuItem (hash=36410781) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=2852357): Activate with root item CommandWithParameterPlugin`1 (hash=19041329)
System.Windows.Data Warning: 108 : BindingExpression (hash=2852357):   At level 0 - for CommandWithParameterPlugin`1.IsVisible found accessor ReflectPropertyDescriptor(IsVisible)
System.Windows.Data Warning: 104 : BindingExpression (hash=2852357): Replace item at level 0 with CommandWithParameterPlugin`1 (hash=19041329), using accessor ReflectPropertyDescriptor(IsVisible)
System.Windows.Data Warning: 101 : BindingExpression (hash=2852357): GetValue at level 0 from CommandWithParameterPlugin`1 (hash=19041329) using ReflectPropertyDescriptor(IsVisible): 'True'
System.Windows.Data Warning: 80 : BindingExpression (hash=2852357): TransferValue - got raw value 'True'
System.Windows.Data Warning: 82 : BindingExpression (hash=2852357): TransferValue - user's converter produced 'Visible'
System.Windows.Data Warning: 89 : BindingExpression (hash=2852357): TransferValue - using final value 'Visible'
CAN EDIT TRUE
CAN EDIT TRUE

我在 Whosebug 中寻找答案,找到了很多建议,但仍然没有解决我的问题。

我认为问题可能出在将绑定上下文命令分配给 CustomContextMenu 时,因为它可能在可视化树之外。解决方案可能是某种代理,但我不知道如何实施它。

要解决通过命令访问 menuItem 可见性以及绑定命令和参数的问题,请将数据模板设置为 MenuItem 样式 由于项目中有多种命令类型选择是通过T​​ypeNameConverter

<Style.Triggers>
    <DataTrigger Binding="{Binding Converter={StaticResource TypeNameConverter}}" Value="CommandType`1">
        <Setter Property="CommandParameter" Value="{Binding SomeParametersFromParent, RelativeSource={RelativeSource AncestorType={x:Type local:CustomContextMenu}}}"/>
        <Setter Property="Command" Value="{Binding Path=Command}"/>
        <Setter Property="Header" Value="{Binding Path=HeaderInfo}"/>
    </DataTrigger>

希望这对遇到类似问题的你们有所帮助