Context Menu Example

rightClickContextMenu.png

The Context Menu is not supported currently as a full-blown IParent. However, there are a few convenience extension methods available for it in ContextMenuExt.

Custom colors may be applied to both the Context Menu and its Context Menu Items via ApplyContextMenuStrategy. As you can see in the example pic above, it does allow checking/unchecking, if desired.

The only thing to be careful of is that the Click, Checked, and Unchecked events are handled via normal .NET events so remember to unsubscribe them, if necessary. You won't be able to leverage the WPF Composite approach of unsubscribing via a DisposeEventsOnAllParents method call.

#region Add Context Menu to Grid

ContextMenu cme = new ContextMenu();
MenuItem item1 = new MenuItem();
MenuItem item2 = new MenuItem();
MenuItem item3 = new MenuItem();
MenuItem item4 = new MenuItem();

item1.Header = "Say Hello";
item1.Click += new RoutedEventHandler((o, e)=>{
                 MessageBox.Show("hello");
            });
cme.Items.Add(item1);

item2.Header = "Scroll to First Item in Grid";
item2.Click += new RoutedEventHandler((o, e)=>{
                scrollToFirstItemInGrid(ccl3dBorder1, masterDetail);
            });
cme.Items.Add(item2);

item3.Header = "Scroll to Last Item in Grid";
item3.Click+= new RoutedEventHandler((o, e)=>{
                scrollToLastItemInGrid(ccl3dBorder1, masterDetail);
            });
item3.IsCheckable = true;
item3.Checked += new RoutedEventHandler((o, e) =>
            {
                MessageBox.Show("I have been checked!");
            });
item3.Unchecked += new RoutedEventHandler((o, e) =>
            {
                MessageBox.Show("I have been unchecked!");
            });
cme.Items.Add(item3);

item4.Header = "Say Goodbye";
item4.Click += new RoutedEventHandler((o, e) =>
           {
                MessageBox.Show("goodbye");
            });
cme.Items.Add(item4);

cme.ApplyContextMenuStrategy("White", "SlateGray", new Thickness(2), "Black", "LightBlue", "LightBlue", "Gray", item1, item2, item3, item4);

masterDetail.ContextMenu = cme;

#endregion

Last edited Jun 14, 2013 at 12:13 AM by stagathome0069, version 4

Comments

No comments yet.