DynamicField
DynamicField

Reputation: 411

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

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 !

Upvotes: 0

Views: 70

Answers (1)

SoronelHaetir
SoronelHaetir

Reputation: 15172

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

Upvotes: 1

Related Questions