我在viewModel中有两个属性用于visiblity ContextMenu和ContextMenuItem。
/// <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 :(得分:0)
我用它。
<ContextMenu x:Key="ItemContextMenu" Visibility="{Binding DataContext.ShowContext,RelativeSource={RelativeSource AncestorType=Window},Converter={StaticResource ToVisibilityConverter}}">
答案 1 :(得分:0)
我想告诉你的第一件事是,如果它是一个MVVM框架,你可以像这样绑定可见性
<ContextMenu x:Key="ItemContextMenu" Visibility="{Binding ShowContext},Converter={StaticResource ToVisibilityConverter}}">
我希望您已将ToVisibilityConverter
放在类文件中