L
L
Lapish722020-08-25 14:33:35
WPF
Lapish72, 2020-08-25 14:33:35

How to disable ContextMenu on child elements?

Is grid on it the collection kontrolov will settle down. I need to make the ContextMenu only work between items. There is such a sketch of the code, but it does not work. What am I doing wrong?

<Grid Background="Transparent">
    <Grid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Добавить"/>
            <MenuItem Header="Удалить"/>
        </ContextMenu>
    </Grid.ContextMenu>

    <StackPanel>
        <Button Content="123" ContextMenuService.IsEnabled="False"/>
    </StackPanel>
</Grid>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lapish72, 2020-08-25
@Lapish72

UserControl.xaml

<UserControl.Resources>
    <ContextMenu x:Key="AmazingMenu">
        <MenuItem Header="Item1"/>
        <MenuItem Header="Item2"/>
    </ContextMenu>
</UserControl.Resources>

<Grid x:Name="MegaGrid" Background="Transparent" MouseRightButtonDown="Grid_MouseRightButtonDown">
    <!-- Some controls -->
</Grid>

UserControl.xaml.cs
private void Grid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    var control = e.OriginalSource as Grid;

    if (control.Name.Equals("MegaGrid"))
    {
        MegaGrid.ContextMenu = FindResource("AmazingMenu") as ContextMenu;
    }
    else
    {
        MegaGrid.ContextMenu = null;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question