C# WPF MVVM绑定对动态菜单项的可见性

C# WPF MVVM绑定对动态菜单项的可见性,c#,wpf,mvvm,binding,prism,C#,Wpf,Mvvm,Binding,Prism,如何在命令CanExecute*方法更改其状态时更新上下文菜单项 我在将可见性绑定到动态创建的MenuItems(基于ICommands和DataTemplates)时遇到问题。 ContextMenu是为绑定一些自定义参数的GridControl创建的。 我通过Freezable代理将这些参数绑定到ContextMenu。 除了CanExecute*不改变MenuItem可视性外,所有操作都正常。 如果CanExecute*具有常量e.CanExecute=true,则可以(menuitem处

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

我在将
可见性
绑定到动态创建的
MenuItems
(基于
ICommands
DataTemplates
)时遇到问题。
ContextMenu
是为绑定一些自定义参数的GridControl创建的。 我通过
Freezable
代理将这些参数绑定到
ContextMenu
。 除了
CanExecute*
不改变
MenuItem
可视性外,所有操作都正常。 如果
CanExecute*
具有常量
e.CanExecute=true
,则可以(menuitem处于活动状态),但当
CanExecute*
具有一些逻辑并且可以同时具有两种状态时,则
menuitem
始终将
IsEnabled
设置为false

一些代码:
ContextCommands是ICommand的扩展

IEnumerable<ICommand<SomeClass>> ContextCommands
数据模板

<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)>
我在stackoverflow中寻找答案,找到了很多建议,但我的问题仍然没有解决


我认为问题可能在于将ContextCommand绑定到CustomContextMenu,因为它可能在visualtree之外。解决方案可能是某种代理,但我不知道如何实现它。

要解决通过命令和绑定命令和参数访问menuItem可见性的问题,需要以menuItem样式设置数据模板 由于项目中有多种命令类型,因此通过TypeNameConverter进行选择

<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>


希望这能帮助你们中的一些人解决类似的问题;可能就是你想要的?我不认为这是问题所在,当调试CanExecute时,我看到它被提出,但对UI没有影响(见我的最后一段)。你累了吗?@机修工是的,我使用它。另外,我更新了上次的输出信息,以显示何时调用CanexExete*以及它试图设置的值。
<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>
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
<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>