Property Menu
- 4 minutes to read
Property Menu Overview
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:
- The Refresh button invokes a bound property getter to refresh the value displayed in PropertyGridControl. This is useful when you edit an object that does not support change notifications.
- The Reset button resets a property to its initial value. This button is enabled when the CanResetValue method of a corresponding property descriptor returns
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.MenuOpening
- Occurs when a user opens the PropertyGridControl‘s menu and allows you to customize it.
- PropertyGridControl.MenuCustomizations
- Specifies the menu customizations for the entire property grid.
- PropertyDefinition.MenuCustomizations
- Specifies 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. |
Add Custom Menu Items
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.
In XAML
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>
In Code
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);
}
}
Remove Existing Menu Items
The following example removes the Reset item from the property menu:
In XAML
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>
In Code
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});
}
}
Disable all the PropertyGridControl’s Menus
If you do not want to display any of the PropertyGridControl‘s menus, follow the steps below:
- Handle the PropertyGridControl.MenuOpening event.
- In the event handler, set the e.Handled property to
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;
}