Keyboard Support
RadPropertyGrid allows you to navigate through the items without using the mouse. The keyboard can entirely replace the mouse by allowing you to perform navigation and editing.
Please note that the keyboard navigation is supported only when RadpropertyGrid's property - RenderMode is set to Flat.
Here is the list of the keys that are supported:
Tab / Shift + Tab - navigates through the items.
Ctrl + Shift + O - sorts by OrderIndex and DisplayName.
Ctrl + Shift + C - groups by GroupName.
Ctrl + Shift + Space - expands the field that represents the current property definition.
Ctrl + End - moves to the last field.
Ctrl + Home - moves to the first field.
PageDown - moves a page down.
PageUp - moves a page up.
Custom Keyboard Command Provider
RadPropertyGrid provides an easy MVVM-friendly approach for customization of its default commands logic. If you want to modify the way RadPropertyGrid handles a particular key, you could create your own custom command provider and predefine the behaviour for that key.
The approach for accomplishing the purpose is to create a separate class, inherit PropertyGridCommandProvider and override the ProvideCommandsForKey(KeyEventArgs args) method. In this way, only the undesired behavior can be adjusted according to your requirements.
The class responsible for customizing the keyboard navigation should be similar to the one below:
Example 1: Creating custom command provider
public class CustomKeyboardCommandProvider : PropertyGridCommandProvider
{
public CustomKeyboardCommandProvider()
: base(null)
{
}
public CustomKeyboardCommandProvider(RadPropertyGrid propertyGrid)
: base(propertyGrid)
{
this.PropertyGrid = propertyGrid;
}
public override List<DelegateCommandWrapper> ProvideCommandsForKey(KeyEventArgs args)
{
List<DelegateCommandWrapper> actionsToExecute = base.ProvideCommandsForKey(args);
if (args.Key == Key.Tab)
{
actionsToExecute.Clear();
actionsToExecute.Add(new PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.MoveToNext, this.PropertyGrid));
if (!this.PropertyGrid.SelectedPropertyDefinition.IsExpanded)
{
actionsToExecute.Add(new PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.ExpandCurrentField, this.PropertyGrid));
}
}
if (args.Key == Key.Tab && Keyboard.Modifiers == ModifierKeys.Shift)
{
actionsToExecute.Clear();
actionsToExecute.Add(new PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.MoveToPrevious, this.PropertyGrid));
if (!this.PropertyGrid.SelectedPropertyDefinition.IsExpanded)
{
actionsToExecute.Add(new PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.ExpandCurrentField, this.PropertyGrid));
}
}
return actionsToExecute;
}
}
Public Class CustomKeyboardCommandProvider
Inherits PropertyGridCommandProvider
Public Sub New()
MyBase.New(Nothing)
End Sub
Public Sub New(ByVal propertyGrid As RadPropertyGrid)
MyBase.New(propertyGrid)
Me.PropertyGrid = propertyGrid
End Sub
Public Overrides Function ProvideCommandsForKey(ByVal args As KeyEventArgs) As List(Of DelegateCommandWrapper)
Dim actionsToExecute As List(Of DelegateCommandWrapper) = MyBase.ProvideCommandsForKey(args)
If args.Key = Key.Tab Then
actionsToExecute.Clear()
actionsToExecute.Add(New PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.MoveToNext, Me.PropertyGrid))
If Not Me.PropertyGrid.SelectedPropertyDefinition.IsExpanded Then
actionsToExecute.Add(New PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.ExpandCurrentField, Me.PropertyGrid))
End If
End If
If args.Key = Key.Tab AndAlso Keyboard.Modifiers = ModifierKeys.Shift Then
actionsToExecute.Clear()
actionsToExecute.Add(New PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.MoveToPrevious, Me.PropertyGrid))
If Not Me.PropertyGrid.SelectedPropertyDefinition.IsExpanded Then
actionsToExecute.Add(New PropertyGridDelegateCommandWrapper(RadPropertyGridCommands.ExpandCurrentField, Me.PropertyGrid))
End If
End If
Return actionsToExecute
End Function
End Class
Following up the code-snippet above, a press of Tab/ Shift + Tab keys will result in moving the focus to the property field above/ below and expanding it if possible. However, do not forget to remove the predefined commands for that particular key by calling the Clear() method.
The last thing to be done is to set CommandProvider Property of the RadPropertyGrid to be the newly-created CustomKeyboardCommandProvider class:
Example 2: Defining the custom command provider in XAML
<telerik:RadPropertyGrid x:Name="RadPropertyGrid">
<telerik:RadPropertyGrid.CommandProvider>
<my:CustomKeyboardCommandProvider PropertyGrid="{Binding ElementName=PropertyGrid1}" />
</telerik:RadPropertyGrid.CommandProvider>
</telerik:RadPropertyGrid>
Example 3: Set the custom command provider to RadPropertyGrid
this.RadPropertyGrid.CommandProvider = new CustomKeyboardCommandProvider(this.RadPropertyGrid);
Me.RadPropertyGrid.CommandProvider = New CustomKeyboardCommandProvider
Disabling Navigation
As of Q1 2015 PropertyGridCommandProvider exposes a new property: EnableBuiltInNavigation. By default its value is set to "True". In order to disable the navigation which comes from "Flat" RenderMode, you can set its value to "False".
Example 4: Disabling navigation
public class CustomKeyboardCommandProviderWithoutNavigation : PropertyGridCommandProvider
{
public CustomKeyboardCommandProviderWithoutNavigation()
: base(null)
{
}
public CustomKeyboardCommandProviderWithoutNavigation(RadPropertyGrid propertyGrid)
: base(propertyGrid)
{
this.PropertyGrid = propertyGrid;
this.EnableBuiltInNavigation = false;
}
}
Public Class CustomKeyboardCommandProviderWithoutNavigation
Inherits PropertyGridCommandProvider
Public Sub New()
MyBase.New(Nothing)
End Sub
Public Sub New(ByVal propertyGrid As RadPropertyGrid)
MyBase.New(propertyGrid)
Me.PropertyGrid = propertyGrid
Me.EnableBuiltInNavigation = False
End Sub
End Class