How do I disable all MenuItems in a ContextMenu in XAML?
I've tried to just Bind to ContextMenu.IsEnabled but that makes it so the ContextMenu stays stuck open even after clicking off of it. Illustrated in this simplified code and screenshot:
<Window x:Class="ContextMenuSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="480"
Width="640">
<Grid>
<Rectangle Width="100"
Height="100"
Fill="Black">
<Rectangle.ContextMenu>
<ContextMenu IsEnabled="False">
<MenuItem Header="Command _1" />
<MenuItem Header="Command _开发者_JS百科2" />
<MenuItem Header="Command _3" />
<MenuItem Header="Command _4" />
</ContextMenu>
</Rectangle.ContextMenu>
</Rectangle>
</Grid>
</Window>
Any suggestions how I would go about disabling all context menu options in simple manner through XAML without the ContextMenu acting all funny?
<ContextMenu>
<ContextMenu.Resources>
<Style TargetType="MenuItem">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</ContextMenu.Resources>
<MenuItem Header="Command _1" />
<MenuItem Header="Command _2" />
<MenuItem Header="Command _3" />
<MenuItem Header="Command _4" />
</ContextMenu>
Should do, reenabling is less amusing though as is, but you could bind the value in the setter to something you can easily access.
Further you can disable the menu at a higher level:
<Rectangle Width="100"
Height="100"
Fill="Black"
ContextMenuService.IsEnabled="False">
<Rectangle.ContextMenu>
<ContextMenu>
<MenuItem Header="Command _1" />
<MenuItem Header="Command _2" />
<MenuItem Header="Command _3" />
<MenuItem Header="Command _4" />
</ContextMenu>
</Rectangle.ContextMenu>
</Rectangle>
Then it will not open at all.
精彩评论