2016-12-27 10 views
0

コマンドCanExecute*メソッドの状態が変更されたときにContext MenuItemを更新する方法。WPF MVVM動的MenuItemへの可視性のバインド

ダイナミックに作成されたvisibilityICommandsおよび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にバインドすることに問題があると想定します。ソリューションは、おそらくそれのためのプロキシのいくつかの種類かもしれないが、私はそれを実装する方法がわからない。

+0

RaiseCanExecuteChanged();あなたが探しているものかもしれませんか? –

+0

CanExecuteをデバッグするときに問題があるとは思わないが、Raisedが表示されているが、UIには何の効果もない(最後の段落を参照) – cichy

+0

[BooleanToVisibilityConverter](https://msdn.microsoft.com/en) -us/library/system.windows.controls.booleantovisibilityconverter(v = vs.110).aspx)? – mechanic

答えて

0

は、コマンドを通じてのMenuItem視界にアクセスし、コマンドとパラメータを結合して、問題を解決するには、プロジェクトの選択で複数のコマンドタイプは、このことができます

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

希望TypeNameConverterを介して行われているので、MenuItemのスタイル にデータテンプレートを設定することでしたあなたと同じような問題を抱えている人もいます。

関連する問題