wpf-15720-controls-and-libraries-property-grid-property-menu.md
The PropertyGridControl can show menu for each property. To enable or disable property menu, use the PropertyDefinition.ShowMenuButton property.
The PropertyGridControl displays the following default menu items:
true. To enable the Reset button for standard non-dependency properties, specify the DefaultValue attribute.The PropertyGridControl allows you to customize property menus. To add new menu items or remove existing items, use the following members:
PropertyGridControl.MenuOpeningOccurs when a user opens the PropertyGridControl‘s menu and allows you to customize it.PropertyGridControl.MenuCustomizationsSpecifies the menu customizations for the entire property grid.PropertyDefinition.MenuCustomizationsSpecifies the menu customizations for individual properties.
Note
The PropertyDefinition.MenuCustomizations property has higher precedence than the PropertyGridControl.MenuCustomizations property.
The following table lists members that allow you to interact with property grid menus in code:
| API | Description |
|---|---|
| ShowPropertyMenu(String) | Invokes the Property Menu for the property definition with the specified path. |
| ShowNewItemMenu(String) | Invokes the New Item Menu for the collection definition with the specified path. |
| IsMenuVisible | Gets whether a property grid menu is open. |
| HideMenu() | Closes the opened property grid menu. |
The following example demonstrates how to add a custom menu item to a property definition and specify the new item’s position:
Refer to the following help topic for more information: The List of Bar Items and Links.
Add a bar item (for example, BarCheckItem) to the PropertyDefinition.MenuCustomizations collection and specify item properties. Attach the BarItemLinkActionBase.ItemLinkIndex property to this item to insert it into a specific position.
<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid">
<!-- ... -->
<dxprg:PropertyDefinition Type="sys:String">
<dxprg:PropertyDefinition.MenuCustomizations>
<dxb:BarCheckItem Content="Checked" IsChecked="True" dxb:BarItemLinkActionBase.ItemLinkIndex="0"/>
<dxb:BarItemLinkSeparator dxb:BarItemLinkActionBase.ItemLinkIndex="1"/>
</dxprg:PropertyDefinition.MenuCustomizations>
</dxprg:PropertyDefinition>
Handle the PropertyGridControl.MenuOpening event.
<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid">
<!-- ... -->
<dxprg:PropertyGridControl ...
MenuOpening="OnMenuOpening">
<dxprg:PropertyDefinition Type="sys:String"/>
</dxprg:PropertyGridControl>
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.PropertyGrid;
// ...
void OnMenuOpening(object sender, PropertyGridMenuEventArgs e) {
if (e.Row.Type == typeof(string)) {
BarCheckItem checkItem = new BarCheckItem {Content = "Checked", IsChecked = true};
BarItemLinkActionBase.SetItemLinkIndex(checkItem, 0);
e.Customizations.Add(checkItem);
BarItemLinkSeparator separator = new BarItemLinkSeparator();
BarItemLinkActionBase.SetItemLinkIndex(separator, 1);
e.Customizations.Add(separator);
}
}
Imports DevExpress.Xpf.Bars
Imports DevExpress.Xpf.PropertyGrid
' ...
Private Sub OnMenuOpening(ByVal sender As Object, ByVal e As PropertyGridMenuEventArgs)
If e.Row.Type = GetType(String) Then
Dim checkItem As BarCheckItem = New BarCheckItem With {
.Content = "Checked",
.IsChecked = True
}
BarItemLinkActionBase.SetItemLinkIndex(checkItem, 0)
e.Customizations.Add(checkItem)
Dim separator As BarItemLinkSeparator = New BarItemLinkSeparator()
BarItemLinkActionBase.SetItemLinkIndex(separator, 1)
e.Customizations.Add(separator)
End If
End Sub
The following example removes the Reset item from the property menu:
Add the RemoveBarItemAndLinkAction object to the PropertyDefinition.MenuCustomizations collection. Assign the menu item name from the BarItemNames class to the ItemName property.
<Window ...
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid">
<!-- ... -->
<dxprg:PropertyDefinition Type="sys:String">
<dxprg:PropertyDefinition.MenuCustomizations>
<dxb:RemoveBarItemAndLinkAction ItemName="{x:Static dxprg:BarItemNames.Reset}"/>
</dxprg:PropertyDefinition.MenuCustomizations>
</dxprg:PropertyDefinition>
Handle the PropertyGridControl.MenuOpening event.
<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid">
<!-- ... -->
<dxprg:PropertyGridControl ...
MenuOpening="OnMenuOpening">
<dxprg:PropertyDefinition Type="sys:String"/>
</dxprg:PropertyGridControl>
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.PropertyGrid;
// ...
void OnMenuOpening(object sender, PropertyGridMenuEventArgs e) {
if (e.Row.Type == typeof(string)) {
e.Customizations.Add(new RemoveBarItemAndLinkAction {ItemName = BarItemNames.Reset});
}
}
Imports DevExpress.Xpf.Bars
Imports DevExpress.Xpf.PropertyGrid
' ...
Private Sub OnMenuOpening(ByVal sender As Object, ByVal e As PropertyGridMenuEventArgs)
If e.Row.Type = GetType(String) Then
e.Customizations.Add(New RemoveBarItemAndLinkAction With {
.ItemName = BarItemNames.Reset
})
End If
End Sub
If you do not want to display any of the PropertyGridControl‘s menus, follow the steps below:
true.<Window ...
xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid">
<!-- ... -->
<dxprg:PropertyGridControl ...
MenuOpening="OnMenuOpening"/>
using DevExpress.Xpf.PropertyGrid;
// ...
void OnMenuOpening(object sender, PropertyGridMenuEventArgs e) {
e.Handled = true;
}
Imports DevExpress.Xpf.PropertyGrid
' ...
Private Sub OnMenuOpening(ByVal sender As Object, ByVal e As PropertyGridMenuEventArgs)
e.Handled = True
End Sub
See Also