Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable the whole Context Menu

I know there is a way to block or now show context menu by using ContextMenuOpening event.

But I still want to show the context menu, just disable everything in it, is there a way to do it?

How can I disable all the menu item at a same time?

<DataTemplate x:Key="ItemDataTemplate">
    <Grid Background="Transparent">
        <Grid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="New" Click="New_Click" />
                <Separator />
                <MenuItem Header="Duplicate" Click="Duplicate_Click"/>
                <MenuItem Header="Delete" Click="Delete_Click"  />
                <MenuItem Header="Rename" Click="Rename_Click" />
                <Separator />
                <MenuItem Header="Export..." Click="Export_Click" />
                <MenuItem Header="Print..."
                          Command="ApplicationCommands.Print"
                          InputGestureText="" />
                <Separator />
                <MenuItem Header="Properties" Click="Properties_Click" />
            </ContextMenu>
        </Grid.ContextMenu>
        <StackPanel Orientation="Horizontal"
                    Margin="0,0,10,0"
                    HorizontalAlignment="Stretch"
                    Background="Transparent"
                    IsHitTestVisible="False">    
        </StackPanel>
    </Grid>    
</DataTemplate>
like image 959
CodingTT Avatar asked Nov 11 '11 20:11

CodingTT


2 Answers

<Style x:Key="z3r0_Style_TextBox_Default" BasedOn="{x:Null}"
    TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="11"/>
<Setter Property="Background" Value="{StaticResource z3r0_SolidColorBrush_DarkerGray}"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
<Setter Property="FontFamily" Value="Consolas"/>
<Setter Property="Foreground" Value="{StaticResource z3r0_SolidColorBrush_Green}"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="MinWidth" Value="10"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ContextMenu">                
    <Setter.Value>
        <ContextMenu IsEnabled="False" Visibility="Hidden">
        </ContextMenu>
    </Setter.Value>
</Setter>

For me, setting IsEnabled to False still caused a small empty context menu to be visible. After changing its visibility attribute, it was truly disabled.

like image 160
Aaron Avatar answered Oct 13 '22 01:10

Aaron


IsEnabled = false on the ContextMenu?

Edit: As there appear to be closing problems when doing this i would suggest a container style:

<ContextMenu>
    <ContextMenu.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="IsEnabled" Value="False"/>
        </Style>
    </ContextMenu.ItemContainerStyle>
    <MenuItem Header="Test"/>
</ContextMenu>

Of course the actual value can be bound as well but in the container style the context is the individual item, so to bind to the parent context a RelativeSource binding is necessary (unless the data object also has a connection to the parent).

like image 39
H.B. Avatar answered Oct 13 '22 00:10

H.B.