Define Custom Trigger Behavior
You can use a custom behavior that will perform additional logic when attaching the RadRadialMenu to the target element. You have to create a class that inherits from the RadialMenuTriggerBehavior class and override its methods to implement custom logic defining where and when the context menu will appear/diappears. The RadRadialMenu is attached/detached to the target element by calling the AttachToTargetElement/DetachToTargetElement method.
This example demonstrates how you can display the context menu when the text in a TextBox is changed and hide it when the Esc
button is pressed.
First, create a class that inherits from the RadialMenuTriggerBehavior class. You can override its SubscribeToTargetEvents and UnsubscribeFromTargetEvents methods to implement your custom logic for triggering the appearance of the menu.
Example 1: Custom RadialMenuTriggerBehavior
public class CustomTriggerBehavior : RadialMenuTriggerBehavior
{
public CustomTriggerBehavior()
{
this.AttachTriggers = RadialMenuAttachTriggers.None;
}
protected override void SubscribeToTargetEvents(FrameworkElement element)
{
base.SubscribeToTargetEvents(element);
var textBox = element as TextBox;
textBox.TextChanged += TextBoxTextChanged;
textBox.KeyDown += TextBoxKeyDown;
}
void TextBoxKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Escape)
{
this.DetachFromTargetElement();
}
}
void TextBoxTextChanged(object sender, TextChangedEventArgs e)
{
this.AttachToTargetElement();
}
protected override void UnsubscribeFromTargetEvents(FrameworkElement element)
{
var textBox = element as TextBox;
textBox.TextChanged -= TextBoxTextChanged;
textBox.KeyDown -= TextBoxKeyDown;
base.UnsubscribeFromTargetEvents(element);
}
}
Example 2: Set the Behavior Property
<TextBox Width="200">
<navigation:RadRadialContextMenu.Behavior>
<local:CustomTriggerBehavior/>
</navigation:RadRadialContextMenu.Behavior>
<navigation:RadRadialContextMenu.Menu>
<navigation:RadRadialMenu/>
</navigation:RadRadialContextMenu.Menu>
</TextBox>