2016-05-21 4 views
-1

visibility ContextMenuとContextMenuItemの2つのプロパティをviewModelに持っています。ListViewItemのContextMenu.Visibilityをviewmodelのプロパティにバインドするにはどうすればよいですか?

/// <summary> 
    /// show context 
    /// </summary> 
    bool _showContext; 
    public bool ShowContext 
    { 
     get { return _showContext; } 
     set 
     { 
      if (value != _showContext) 
      { 
       _showContext = value; 
       RaisePropertyChanged("ShowContext"); 
      } 
     } 
    } 

     /// <summary> 
    /// can archive 
    /// </summary> 
    bool _isArchiveContext; 
    public bool IsArchiveContext 
    { 
     get { return _isArchiveContext; } 
     set 
     { 
      if (value != _isArchiveContext) 
      { 
       _isArchiveContext = value; 
       RaisePropertyChanged("IsArchiveContext"); 
      } 
     } 
    } 

とXamlにおいて、私は2つの結合方法を使用する。しかし、束縛しないでください。

<ContextMenu x:Key="ItemContextMenu" Visibility="{Binding PlacementTarget.ShowContext,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource ToVisibilityConverter}}"> 
<MenuItem Header=" بایگانی" 
     Command="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ArchiveCommand}" Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=IsArchiveContext,Converter={StaticResource ToVisibilityConverter}}" 
     CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=SelectedItems}" /> 




<ListView.ItemContainerStyle> 
    <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource ListViewItemStyle}"> 
    <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" /> 
    </Style> 
</ListView.ItemContainerStyle> 

答えて

0

私はこれを使用します。私はあなたに伝えたい

<ContextMenu x:Key="ItemContextMenu" Visibility="{Binding DataContext.ShowContext,RelativeSource={RelativeSource AncestorType=Window},Converter={StaticResource ToVisibilityConverter}}"> 
0

最初はそのMVVMフレームワークは、あなたがこの

<ContextMenu x:Key="ItemContextMenu" Visibility="{Binding ShowContext},Converter={StaticResource ToVisibilityConverter}}"> 

のような可視性をバインドすることができれば、私はあなたがクラスファイル

ToVisibilityConverterすでに持っていることを願っていることです
+0

ありがとうございますが、 'ContextMenu'はListViewItem用ですので、' Binding ShowContext'を使用しないでください。私は 'RelativeSource'を使います。 –

関連する問題