Example T325071
Visible to All Users

Create a Radial Context Menu for WPF

This example demonstrates how to create a WPF Radial Context Menu and assign it to a text box.

WPF Radial Context Menu, DevExpress

Implementation Details

The radial menu's DataContext is bound to the window's DataContext. The DataContext is set to a RadialContextMenuViewModel object (which is automatically generated by DevExpress.Mvvm.POCO.ViewModelSource). This object automatically generates commands for all public methods in the RadialContextMenuViewModel class.

The radial menu displays four items: "Copy", "Cut", "Paste", and "Select All". The "Select All" item is a sub-menu with "Clear" and "Select All" items.

The item's Command property specifies the command:

XAML
<dxb:BarButtonItem Content="Copy" Glyph="{dx:DXImage Image=Copy_16x16.png}" Command="{Binding CopyCommand}" CommandTarget="{Binding}" />

Use the BarManager.DXContextMenu property to assign the radial context menu to the text box.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

RadialMenuExample/MainWindow.xaml
XAML
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:RadialMenuExample" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" x:Class="RadialMenuExample.MainWindow" Title="MainWindow" Height="350" Width="525" DataContext="{dxmvvm:ViewModelSource Type=local:RadialContextMenuViewModel}"> <Grid> <TextBox HorizontalAlignment="Center" VerticalAlignment="Center" Width="250" Text="DevExpress"> <dxmvvm:Interaction.Behaviors> <local:TextBoxService /> </dxmvvm:Interaction.Behaviors> <dxb:BarManager.DXContextMenu > <dxb:RadialContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"> <dxb:BarButtonItem Content="Copy" Glyph="{dx:DXImage Image=Copy_16x16.png}" Command="{Binding CopyCommand}" CommandTarget="{Binding}" /> <dxb:BarButtonItem Content="Cut" Glyph="{dx:DXImage Image=Cut_16x16.png}" Command="{Binding CutCommand}" CommandTarget="{Binding}" /> <dxb:BarButtonItem Content="Paste" Glyph="{dx:DXImage Image=Paste_16x16.png}" Command="{x:Static ApplicationCommands.Paste}" CommandTarget="{Binding}" /> <dxb:BarSplitButtonItem Content="Select All" Glyph="{dx:DXImage Image=SelectAll_16x16.png}" LargeGlyph="{dx:DXImage Image=SelectAll_32x32.png}" Command="{Binding SelectAllCommand}" > <dxb:BarSplitButtonItem.PopupControl> <dxb:PopupMenu> <dxb:BarButtonItem x:Name="bClear" CategoryName="Edit" Content="Clear" Glyph="{dx:DXImage Image=Delete_16x16.png}" Command="{Binding ClearSelectionCommand}" /> <dxb:BarButtonItem Content="Select All" Glyph="{dx:DXImage Image=SelectAll_16x16.png}" LargeGlyph="{dx:DXImage Image=SelectAll_32x32.png}" Command="{Binding SelectAllCommand}" /> </dxb:PopupMenu> </dxb:BarSplitButtonItem.PopupControl> </dxb:BarSplitButtonItem> </dxb:RadialContextMenu> </dxb:BarManager.DXContextMenu> </TextBox> </Grid> </Window>
RadialMenuExample/TextBoxService.cs(vb)
C#
using DevExpress.Mvvm.UI; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; namespace RadialMenuExample { public interface ITextBoxService { void ClearSelection(); bool CanClearSelection(); } //This service is used for the Clear command. To learn more about custom services, refer to https://documentation.devexpress.com/#WPF/CustomDocument16920 public class TextBoxService : ServiceBase, ITextBoxService { TextBox MyTextBox { get { return AssociatedObject as TextBox; } } public void ClearSelection() { MyTextBox.SelectionLength = 0; } public bool CanClearSelection() { return MyTextBox.SelectionLength > 0; } } }
RadialMenuExample/ViewModel.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace RadialMenuExample { public class RadialContextMenuViewModel { public virtual ITextBoxService TextBoxService { get { return null; } } public ICommand CopyCommand { get; set; } public ICommand PasteCommand { get; set; } public ICommand CutCommand { get; set; } public ICommand SelectAllCommand { get; set; } public RadialContextMenuViewModel() { CopyCommand = ApplicationCommands.Copy; PasteCommand = ApplicationCommands.Paste; CutCommand = ApplicationCommands.Cut; SelectAllCommand = ApplicationCommands.SelectAll; } // A ClearSelectionCommand is automatically created from the following methods by POCO: public bool CanClearSelection() { return TextBoxService.CanClearSelection(); } public void ClearSelection() { TextBoxService.ClearSelection(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.