【问题标题】:WPF MVVM Binding visibility to dynamic MenuItemWPF MVVM 绑定到动态 MenuItem 的可见性
【发布时间】:2017-05-11 18:45:19
【问题描述】:

当命令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 的可见性? 我试图通过 DataContext 但没有效果,UI 没有改变 调试此绑定表明 Visibility 设置正确,但没有效果。

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

我在 stackoverflow 中寻找答案,找到了很多建议,但我的问题仍然没有运气。

我认为问题可能在于将绑定 ContextCommands 分配给 CustomContextMenu,因为它可能在可视化树之外。解决方案可能是某种代理,但我不知道如何实现它。

【问题讨论】:

  • RaiseCanExecuteChanged();可能是你要找的?
  • 我不认为这是问题所在,在调试 CanExecute 时,我看到它已引发,但对 UI 没有影响(请参阅我的最后一段)
  • @mechanic 是的,我使用它。 PS。我更新了最后的输出信息,以显示何时调用 CanExexute* 以及它尝试设置的值。

标签: c# wpf mvvm binding prism


【解决方案1】:

要解决通过命令和绑定命令和参数访问 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>

希望这可以帮助一些有类似问题的人

【讨论】:

    猜你喜欢
    • 2011-07-17
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多