WPF [SOLVED]: How to show context menu on a ListBoxItem template on a grid when right-clicking outside the contents of it?

WPF [SOLVED]: How to show context menu on a ListBoxItem template on a grid when right-clicking outside the contents of it?

Home Forums Frameworks WPF WPF [SOLVED]: How to show context menu on a ListBoxItem template on a grid when right-clicking outside the contents of it?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246872

    Cloudy Point
    Keymaster

    QuestionQuestion

    Hello,

    I have a ListBox.ItemTemplate (containing obviously a DataTemplate) containing a Grid which itself contains a Label.

    The Grid has a Grid.ContextMenu. The issue is that the ContextMenu only shows when I right click on the Label. Not outside.

    Here is an image to show the problem if you didn’t understand (But I am sure you did 😉 !)

    Here is the code :

    <ListBox.ItemTemplate>
       <DataTemplate>
          <Grid Margin="0,0,0,1">
             <Grid.ContextMenu>
                <ContextMenu>
                   <MenuItem CommandParameter="{Binding}" Command="{Binding Source={x:Reference window}, Path=DataContext.DeleteCommand}" Header="Supprimer">
                      <MenuItem.Icon>
                         <Image Source="/SchoolTools.Teacher;component/Images/DeleteIcon.png" />
                      </MenuItem.Icon>
                   </MenuItem>
                </ContextMenu>
             </Grid.ContextMenu>
             <Label Content="{Binding ClassYear}" Padding="0" 
              HorizontalAlignment="Center" />
          </Grid>
       </DataTemplate>
    </ListBox.ItemTemplate>
    

    Thanks !

    #246873

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Change the label’s HorizontalAlignment to Stretch so that it fills the entire grid cell.

    Source: https://stackoverflow.com/questions/47954074/how-to-show-context-menu-on-a-listboxitem-template-on-a-grid-when-right-clicking
    Author: SoronelHaetir
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.