WPF hide MenuItem on ContextMenu based on object property

I need to show / hide MenuItem programmatically, what would be the best way to do this?

+5
source share
2 answers

Well, to add a MenuItem, you will need something in this direction:

var menuItem = new MenuItem() { Header = "Menu Name", Name = "Identifier", IsCheckable = true, IsChecked = visible };
menuItem.Click += new RoutedEventHandler(contextMenu_onClick);
int position = contextMenu.Items.Add(menuItem);

(but you probably already got this).

You will need some way to bind the menu item to a property - but without viewing your application, I cannot offer a better way. There is a Tag property that stores the object; Uid property that stores the string; Name property, which also saves the string.

While:

menuItem.Visibility = Visibility.Visible;

and

menuItem.Visibility = Visibility.Collapsed;

should switch the visibility of the element.

: Collapsed , - . ( Botz3000 )

, , , /, , . , :

menuItem.Visibility = menuItem.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
+5

, MenuItem? , WPF:

<MenuItem Header="_MenuName" Command="{x:Static local:MyCommands.SomeCommand}" />

...

<!-- In the menu item or any of its ancestors: -->
<SomeControl.CommandBindings>
    <CommandBinding Command="{x:Static local:MyCommands.SomeCommand}" Executed="Save_Executed" CanExecute="Save_CanExecture" />
</SomeControl.CommandBindings>

WPF bool Save_CanExecute, , MenuItem, / MenuItem.

0

All Articles