Right-click options menu selected in datagrid

Asked

Viewed 944 times

2

Hello, I have the following problem: I have several records on datagrid and I want the moment I select a record and right-click on it, a menu with options for the selected row appears. The only options in this menu is to delete, print and detail. I tried a solution that gave me another question I asked, but it did not work, because it enable the menu anywhere in the datagrid that I click with the right mouse button.

2 answers

2


So that the Contextmenu appears only when a selected line is added Itemcontainerstyle to his Contextmenu

<ContextMenu.ItemContainerStyle>
    <Style TargetType="{x:Type MenuItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems.Count}" Value="0">
                <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems.Count}" Value="1">
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ContextMenu.ItemContainerStyle>  

The right thing would be Contextmenu appear with your items disabled:

<ContextMenu.ItemContainerStyle>
    <Style TargetType="{x:Type MenuItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems.Count}" Value="0">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems.Count}" Value="1">
                <Setter Property="IsEnabled" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ContextMenu.ItemContainerStyle>
  • It did not work, in the part "Path=Placementtarget.SelectedItems.Count" appears this alert: "Cannot resolve Property "Selecteditems" in data context of type "System.Windows.Uielement" :(

  • This happens because sometimes in design mode he can’t follow the path. Run the program it should work.

  • Look what it looked like after adding: http://postimg.org/image/e8bhlhdeb/ and http://postimg.org/image/skg16llw1/

  • That rectangle that appears is the Menu? In the test I did the Menu did not appear. I tested in a Listbox but the result should be the same in Datagrid

  • Yes, the rectangle is what should be on the menu. I don’t understand why it looked like this. Anyway, thanks for the help :)

0

You need to create a ContextMenu on your datagrid and put options on it. It goes something like this:

<DataGrid Name="dataGrid">
    <DataGrid.ContextMenu>
       <ContextMenu>
          <MenuItem Header="Excluir" Click="MenuItemExcluir_Click"/>
          <MenuItem Header="Detalhes" Click="MenuItemDetalhes_Click"/>
          <MenuItem Header="Imprimir" Click="MenuItemImprimir_Click"/>
       </ContextMenu>
    </DataGrid.ContextMenu>
</DataGrid>

For each action of this menu will have a Click event, in it you will be able to identify which item was clicked and continue processing what you need to do.

  • This way I had done it. What I want is for the menu to appear the moment I select a line and right-click on it. So there, anywhere in the datagrid I right click, the menu appears, regardless of the line selected or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.