仅在变量为true时显示上下文菜单

时间:2018-07-31 09:11:23

标签: wpf vb.net

我的.xaml文件中有这段代码:

  <Window.Resources>
  <ContextMenu x:Key="cm">
        <MenuItem Header="Blokiraj" Click="MenuItem_Click" />
        <MenuItem Header="Deblokiraj" Click="MenuItem2_Click"/>
        <MenuItem Header="-------------"/>
        <MenuItem Header="Sakrij" Click="MenuItem3_Click"/>
        <MenuItem Header="Prikaži" Click="MenuItem4_Click"/>
    </ContextMenu>
    <Style TargetType="Button">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
    <Style TargetType="TextBox">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
    <Style TargetType="Label">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
    <Style TargetType="ComboBox">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
    <Style TargetType="dxe:CheckEdit">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
    <Style TargetType="dx:SimpleButton">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
    <Style TargetType="dxb:BarCheckItem">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
    <Style TargetType="dxb:BarSubItem">
        <Setter Property="ContextMenu" Value="{StaticResource cm}" />
    </Style>
   </Window.Resources>

仅当我进入由单个布尔值定义的管理模式时,此上下文菜单才应显示在窗口的每个控件上。布尔值是Globals.admin = True / False。

因此,如果我定义Globals.admin = false,则不应显示上下文菜单“ cm”,因为它会干扰我的其他上下文菜单,这些菜单对于表单上的每个控件都是不同的。

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助。我不完全知道您的项目特殊性,但是我认为以下代码将为您提供帮助。

xaml代码:

<Window.Resources>

    <ContextMenu x:Key="cm">
        <MenuItem Header="Blokiraj"  />
        <MenuItem Header="Deblokiraj" />
        <MenuItem Header="-------------"/>
        <MenuItem Header="Sakrij" />
        <MenuItem Header="Prikaži" />
    </ContextMenu>

    <Style TargetType="FrameworkElement" x:Key="ControlWithContextMenuStyle">
        <Setter Property="ContextMenu" Value="{x:Null}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Globals.admin, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="True">
                <Setter Property="ContextMenu" Value="{StaticResource cm}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>
<Grid>
    <Button Style="{StaticResource ControlWithContextMenuStyle}"
            Content="Click me" Click="Button_Click"
            Width="100" Height="50"
            HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>

xaml.cs背后的代码:

 public partial class MainWindow : Window
{

    public static readonly DependencyProperty GlobalsProperty = DependencyProperty.Register("Globals", typeof(GlobalVariables), typeof(MainWindow), new FrameworkPropertyMetadata(null));

    public GlobalVariables Globals
    {
        get { return (GlobalVariables)GetValue(GlobalsProperty); }
        set { SetValue(GlobalsProperty, value); }
    }

    public MainWindow()
    {
        InitializeComponent();

        Globals = new GlobalVariables();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Globals.admin = true;
    }
}

和GlobalVariables类:

  public class GlobalVariables : INotifyPropertyChanged
{
    private Boolean _admin;

    public Boolean admin
    {
        get { return _admin; }
        set
        {
            _admin = value;
            RaisePropertyChanged("admin");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

Framework Element样式对于您仅制作一种样式,然后可以在需要时将其继承的样式很有用。您可以更改样式中绑定的方式,以适应您的项目需求。