コマンドCanExecute*
メソッドの状態が変更されたときにContext MenuItemを更新する方法。WPF MVVM動的MenuItemへの可視性のバインド
ダイナミックに作成されたvisibility
(ICommands
およびDataTemplates
に基づいて)にバインディングに関する問題があります。 ContextMenu
がカスタムパラメータをバインドするGridControl用に作成されます。 Freezable
代理人を介してこれらのパラメータをContextMenu
にバインドすることができました。 CanExecute*
を除いて、すべてが正常に動作します。MenuItem
可視性は変わりません。それはOKだより CanExecute*
場合には、一定のe.CanExecute = true
持っている(メニュー項目がアクティブである)が、CanExecute*
は、いくつかのロジックを持っているとMenuItem
よりも、両方の状態を持つことができたときには常にいくつかのコード偽
からIsEnabled
セットを持っている:
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)>
グリッド宣言CanExecuteが変化したときに、動的のMenuItemに可視性を起動する方法
<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>
? 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
私は答えのためにstackoverflowを見て、多くの提案を見つけましたが、私の問題はまだ運がありません。
VisualContextの外にある可能性があるので、ContextCommandsをCustomContextMenuにバインドすることに問題があると想定します。ソリューションは、おそらくそれのためのプロキシのいくつかの種類かもしれないが、私はそれを実装する方法がわからない。
RaiseCanExecuteChanged();あなたが探しているものかもしれませんか? –
CanExecuteをデバッグするときに問題があるとは思わないが、Raisedが表示されているが、UIには何の効果もない(最後の段落を参照) – cichy
[BooleanToVisibilityConverter](https://msdn.microsoft.com/en) -us/library/system.windows.controls.booleantovisibilityconverter(v = vs.110).aspx)? – mechanic