如何在命令CanExecute*
方法更改其状态时更新上下文菜单项。WPF MVVM绑定动态菜单项的可见性
我有一个关于绑定visibility
动态创建MenuItems
(基于ICommands
和DataTemplates
)的问题。 ContextMenu
是为GridControl创建的,它绑定了一些自定义参数。 我设法将这些参数通过Freezable
代理绑定到ContextMenu
。 所有作品几乎没有问题,但CanExecute*
不变MenuItem
可见度。 如果CanExecute*
有恒定的e.CanExecute = true
,比它的OK(菜单项处于激活状态),但是当CanExecute*
有一些逻辑,可以有两种状态,低于MenuItem
总是IsEnabled
设置为false
一些代码:
ContextCommands是ICommand的延伸
IEnumerable<ICommand<SomeClass>> ContextCommands
CustomMenuItem
//CustomMenuItem is just extension of MenuItem
public class CustomMenuItem : MenuItem
逸aTemplates
<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变化是什么时候? 我试图通过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,因为它可能在Visualtree之外。解决方案可能可能是某种代理,但我不知道如何实现它。
RaiseCanExecuteChanged();可能是你在找什么? –
我不认为这是问题,当调试CanExecute我看到它的提出,但没有对UI的影响(请参阅我的最后一段) – cichy
你累了[BooleanToVisibilityConverter](https://msdn.microsoft.com/en -us /库/ system.windows.controls.booleantovisibilityconverter(v = vs.110)的.aspx)? – mechanic