2016-05-21 3 views
-1

У меня есть два свойства в представленииModel для visiblity ContextMenu и ContextMenuItem.Как привязать ContextMenu.Visibility к элементу ListViewItem к свойству 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, я использую два метода привязки. Но не обязательны.

<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'. –

Смежные вопросы