XAML - ContextMenu



A ContextMenu represents a pop-up menu that enables a control to expose a functionality that is specific to the context of a control. It appears whenever the context menu is requested through a user interface from within this element. The hierarchical inheritance of ContextMenu class is as follows −

ContextMenu Hierarchy

Properties

Sr.No. Property & Description
1

Background

Gets or sets a brush that provides the background of the control. (Inherited from Control)

2

BorderThickness

Gets or sets the border thickness of a control. (Inherited from Control)

3

ContextMenu

Gets or sets the context menu element that should appear whenever the context menu is requested through user interface (UI) from within this element. (Inherited from FrameworkElement.)

4

FontFamily

Gets or sets the font used to display text in the control. (Inherited from Control)

5

FontSize

Gets or sets the size of the text in this control. (Inherited from Control)

6

FontStyle

Gets or sets the style in which the text is rendered. (Inherited from Control)

7

FontWeight

Gets or sets the thickness of the specified font. (Inherited from Control)

8

Foreground

Gets or sets a brush that describes the foreground color. (Inherited from Control)

9

GroupStyle

Gets a collection of GroupStyle objects that define the appearance of each level of groups. (Inherited from ItemsControl)

10

HasItems

Gets a value that indicates whether the ItemsControl contains items. (Inherited from ItemsControl.)

11

Height

Gets or sets the suggested height of a FrameworkElement. (Inherited from FrameworkElement)

12

HorizontalAlignment

Gets or sets the horizontal alignment characteristics that are applied to a FrameworkElement when it is composed in a layout parent, such as a panel or items control. (Inherited from FrameworkElement)

13

IsFocused

Gets a value that determines whether this element has logical focus. This is a dependency property. (Inherited from UIElement.)

14

IsOpen

Gets or sets a value that indicates whether the ContextMenu is visible.

15

IsEnabled

Gets or sets a value indicating whether the user can interact with the control. (Inherited from Control)

16

ItemsSource

Gets or sets an object source used to generate the content of the ItemsControl. (Inherited from ItemsControl)

17

Margin

Gets or sets the outer margin of a FrameworkElement. (Inherited from FrameworkElement)

18

Name

Gets or sets the identifying name of the object. When a XAML processor creates the object tree from XAML markup, run-time code can refer to the XAML-declared object by this name. (Inherited from FrameworkElement)

19

Opacity

Gets or sets the degree of the object's opacity. (Inherited from UIElement)

20

Style

Gets or sets an instance Style that is applied for this object during layout and rendering. (Inherited from FrameworkElement)

21

VerticalAlignment

Gets or sets the vertical alignment characteristics that are applied to a FrameworkElement when it is composed in a parent object such as a panel or items control. (Inherited from FrameworkElement)

22

Width

Gets or sets the width of a FrameworkElement. (Inherited from FrameworkElement)

Methods

Sr.No. Method & Description
1

AddChild

Adds the specified object as the child of the ItemsControl object. (Inherited from ItemsControl.)

2

Arrange

Positions child objects and determines a size for a UIElement. Parent objects that implement custom layout for their child elements should call this method from their layout override implementations to form a recursive layout update. (Inherited from UIElement)

3

FindName

Retrieves an object that has the specified identifier name. (Inherited from FrameworkElement)

4

Focus

Attempts to set the focus on the control. (Inherited from Control)

5

GetValue

Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject)

6

IsItemItsOwnContainer

Determines if the specified item is (or is eligible to be) its own container. (Inherited from ItemsControl.)

7

OnDragEnter

Called before the DragEnter event occurs. (Inherited from Control)

8

OnDragLeave

Called before the DragLeave event occurs. (Inherited from Control)

9

OnDragOver

Called before the DragOver event occurs. (Inherited from Control)

10

OnDrop

Called before the Drop event occurs. (Inherited from Control)

11

OnContextMenuOpening

Invoked whenever an unhandled ContextMenuClosing routed event reaches this class in its route. Implement this method to add class handling for this event. (Inherited from FrameworkElement.)

12

OnItemsChanged

Invoked when the Items property changes. (Inherited from ItemsControl.)

13

OnLostFocus

Called before the LostFocus event occurs. (Inherited from Control)

14

ReadLocalValue

Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject)

15

SetBinding

Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement)

16

SetValue

Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject)

Events

Sr.No. Event & Description
1

Closed

Occurs when a particular instance of a ContextMenu closes.

2

ContextMenuClosing

Occurs just before any context menu on the element is closed. (Inherited from FrameworkElement.)

3

ContextMenuOpening

Occurs when any context menu on the element is opened. (Inherited from FrameworkElement.)

4

DataContextChanged

Occurs when the data context for this element changes. (Inherited from FrameworkElement.)

5

DragEnter

Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement)

6

DragLeave

Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)

7

DragOver

Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)

8

Drop

Occurs when the input system reports an underlying drop event with this element as the drop target. (Inherited from UIElement)

9

GotFocus

Occurs when a UIElement receives focus. (Inherited from UIElement)

10

IsEnabledChanged

Occurs when the IsEnabled property changes. (Inherited from Control)

11

KeyDown

Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)

12

KeyUp

Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)

13

LostFocus

Occurs when a UIElement loses focus. (Inherited from UIElement)

Example

The following example contains a textbox with a ContextMenu which manipulates the text inside the textbox.

Here is the XAML code in which a TextBox, a ContextMenu, and MenuItems have been created with some properties and events.

<Window x:Class = "XAMLContextMenu.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid> 
      <TextBox Name = "textBox1" TextWrapping = "Wrap" Margin = "10" Grid.Row = "7"> 
         Hi, this is XAML tutorial.
			
         <TextBox.ContextMenu> 
            <ContextMenu> 
               <MenuItem Header = "_Bold" IsCheckable = "True" 
                  Checked = "Bold_Checked" Unchecked = "Bold_Unchecked" /> 
               <MenuItem Header = "_Italic" IsCheckable = "True" 
                  Checked = "Italic_Checked" Unchecked = "Italic_Unchecked" /> 
               <Separator /> 
               <MenuItem Header = "Increase Font Size" Click = "IncreaseFont_Click" /> 
               <MenuItem Header = "_Decrease Font Size" Click = "DecreaseFont_Click" />
            </ContextMenu> 
         </TextBox.ContextMenu> 
      </TextBox> 
      
   </Grid> 
</Window>

Here is the implementation in C# for different events.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data;

namespace XAMLContextMenu { 
   /// <summary> 
      /// Interaction logic for MainWindow.xaml 
   /// </summary> 
	
   public partial class MainWindow : Window { 
      public MainWindow() { 
         InitializeComponent(); 
      } 
      private void Bold_Checked(object sender, RoutedEventArgs e) { 
         textBox1.FontWeight = FontWeights.Bold;
      }
      private void Bold_Unchecked(object sender, RoutedEventArgs e) { 
         textBox1.FontWeight = FontWeights.Normal; 
      }
      private void Italic_Checked(object sender, RoutedEventArgs e) { 
         textBox1.FontStyle = FontStyles.Italic; 
      }
      private void Italic_Unchecked(object sender, RoutedEventArgs e) { 
         textBox1.FontStyle = FontStyles.Normal; 
      }
      private void IncreaseFont_Click(object sender, RoutedEventArgs e) { 
         if (textBox1.FontSize < 18) { 
            textBox1.FontSize += 2; 
         } 
      }
      private void DecreaseFont_Click(object sender, RoutedEventArgs e) { 
         if (textBox1.FontSize > 10) { 
            textBox1.FontSize -= 2; 
         } 
      } 
   } 
}

When you compile and execute the above code, it will produce the following screen −

ContextMenu Output

We recommend you to execute the above example code and experiment with some other properties and events.

xaml_controls.htm
Advertisements