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" :(
– Leomar de Souza
This happens because sometimes in design mode he can’t follow the path. Run the program it should work.
– ramaral
Look what it looked like after adding: http://postimg.org/image/e8bhlhdeb/ and http://postimg.org/image/skg16llw1/
– Leomar de Souza
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
– ramaral
Yes, the rectangle is what should be on the menu. I don’t understand why it looked like this. Anyway, thanks for the help :)
– Leomar de Souza