We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Customizing context menu

Hi guys,

Is it possible to customize the context menu, i.e. when user rightclicks on the node. I'd like to add a bunch of my actions there.
Or perhaps use custom popup menu.

Thanks

7 Replies

NA Nikhil A Syncfusion Team July 9, 2009 10:19 AM UTC

Hi Miha,

Yes it is possible to customize the context menu of the nodes by overriding the MouseRightButtonUp event of the nodes as follows:

foreach (Node nd in d1.Model.Nodes)
{
nd.MouseRightButtonUp += new MouseButtonEventHandler(nd_MouseRightButtonUp);
}

//MouseRightButtonUp event handler for nodes in Diagram 1
void nd_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{

ContextMenu menu = new ContextMenu();

//Action 1
MenuItem m = new MenuItem();
m.Header = "Option1";
m.Click += new RoutedEventHandler(m1_Click);
menu.Items.Add(m);

//Action 2
MenuItem m2 = new MenuItem();
m2.Header = "Create node in Diagram 2";
m2.Click += new RoutedEventHandler(m_Click);
menu.Items.Add(m2);

(sender as Node).ContextMenu = menu;

}

This will apply the context menu for all the nodes.

Please let me know if you have any concerns.

Regards,
Nikhil


MM Miha Markic July 13, 2009 08:59 AM UTC

Hi Nikhil,

Well, I've added this code (at the end of the Window1 constructor in Features demo 2008). But still, I get the default menu instead of that custom one.
I am sure I am missing something simple...




...
foreach (Node nd in diagramModel.Nodes)
{
nd.MouseRightButtonUp += new MouseButtonEventHandler(nd_MouseRightButtonUp);
...

void nd_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
ContextMenu menu = new ContextMenu();

//Action 1
MenuItem m = new MenuItem();
m.Header = "Option1";
// m.Click += new RoutedEventHandler(m1_Click);
menu.Items.Add(m);

//Action 2
MenuItem m2 = new MenuItem();
m2.Header = "Create node in Diagram 2";
// m2.Click += new RoutedEventHandler(m_Click);
menu.Items.Add(m2);

(sender as Node).ContextMenu = menu;
}


NA Nikhil A Syncfusion Team July 13, 2009 10:07 AM UTC

Hi Miha,

Your explanation of the way you have specified the context menu sounds to be correct. There seems to be no reason for it not to work . We have specified the context menu in a similar way as you said in our FeaturesDemo sample and it seems to work fine on our side. Make sure you have specified this after the createNodes() method call. Here is what we did.

public Window1()
{

InitializeComponent();

//SymbolPaletteFilter creates a filter for the palette groups.
SymbolPaletteFilter sfilter = new SymbolPaletteFilter();
sfilter.Label = "Custom";
diagramControl.SymbolPalette.SymbolFilters.Add(sfilter);

//SymbolPaletteGroup creates a group and assigns a specific filter index.
SymbolPaletteGroup s = new SymbolPaletteGroup();
s.Label = "Custom";
SymbolPalette.SetFilterIndexes(s, new Int32Collection(new int[] { 0, 6 }));
diagramControl.SymbolPalette.SymbolGroups.Add(s);

//SymbolPaletteItem specifies the item which can be added to the group.
SymbolPaletteItem ss = new SymbolPaletteItem();
Path path = App.Current.Resources["CustomShape"] as Path;
ss.Content = path;
s.Items.Add(ss);



zoomfactor.SelectedIndex = 0;
//Defines the nodes and adds them to the model.
createNodes();

//Specifies the events.
events();


foreach (Node nd in diagramModel.Nodes)
{
nd.MouseRightButtonUp += new MouseButtonEventHandler(nd_MouseRightButtonUp);
}

}

#endregion


void nd_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
ContextMenu menu = new ContextMenu();

//Action 1
MenuItem m = new MenuItem();
m.Header = "Option1";
// m.Click += new RoutedEventHandler(m1_Click);
menu.Items.Add(m);

//Action 2
MenuItem m2 = new MenuItem();
m2.Header = "Create node in Diagram 2";
// m2.Click += new RoutedEventHandler(m_Click);
menu.Items.Add(m2);

(sender as Node).ContextMenu = menu;
}



Please ensure if you have specified it in this way and let us know if you still face any problem.

Regards,
Nikhil


MM Miha Markic July 13, 2009 01:24 PM UTC

Ok, that's weird. I still think I am missing something obvious. Attached is my (= your) code that yields the standard Order/Grouping/Delete right click context menu for me.
The MouseRightButtonUp method gets executed every right click though.
I am using latest Diagram version (7.203.0.37)/VS2008SP1/XP x86.

Does this code work for you?



App.xaml_c6fd5542.zip


NA Nikhil A Syncfusion Team July 13, 2009 02:36 PM UTC

Hi Miha,

Yes we are able to reproduce this issue in your sample. Here is the modified event handler. Setting coxtextmenu.IsOpen =true will display the custom context menu.

Modified code:

void nd_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
ContextMenu menu = new ContextMenu();

//Action 1
MenuItem m = new MenuItem();
m.Header = "Option1";
// m.Click += new RoutedEventHandler(m1_Click);
menu.Items.Add(m);

//Action 2
MenuItem m2 = new MenuItem();
m2.Header = "Create node in Diagram 2";
// m2.Click += new RoutedEventHandler(m_Click);
menu.Items.Add(m2);

(sender as Node).ContextMenu = menu;

(sender as Node).ContextMenu.IsOpen = true;

}


However this will cause a slight delay in opening the custom menu. This has been fixed in our latest release of Essential Diagram (Essential Studio Volume 3 RC 7.3.0.18). We suggest you to install the latest build and the context menu will get displayed appropriately.

Please refer to the following forum for download details.

http://www.syncfusion.com/support/forums/general/86322/essential-studio-2009-vol3-rc-v73018-available-for-download

In this release , support for Alignment commands, Spacing and Sizing commands, SymbolPalette customization properties also have been provided.


We have noticed you've submitted a number of specific technical inquiries here in the Forums which seem to indicate you are a current customer rather than an evaluator or tester. If so, you might like to know that you have an existing personal Direct-Trac account available using your own email address, and we highly recommend using your personal account. You'll get a faster response along with an automatic update when your question is answered via DirectTrac, and you can login and view your incident history 24x7. Simply visit https://www.syncfusion.com/Support/DirectTrac/logon.aspx?URL=/Support/DirectTrac/default.aspx to
login. If you have any further questions, we hope you will take advantage of your existing account and available services as a license holder.


Please let us know if you have any concerns.

Regards,
Nikhil


PV Pavel Vykhryst September 22, 2009 01:16 PM UTC

Workarround

Assign context menu inside ContextMenuOpening event handler - it works


Node node = new Node();

node.ContextMenuOpening += Node_ContextMenuOpening;

void Node_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var node = sender as Node;
if (node != null && ContextMenu != null)
{
ContextMenu menu = new ContextMenu();

// TODO Add menu items

node.ContextMenu = menu;

// optionally we can keen node for menuitem click handler
foreach (var item in node.ContextMenu.Items)
{
((MenuItem)item).Tag = node;
}
}
}


NA Nikhil A Syncfusion Team September 23, 2009 09:47 AM UTC

Hi ,

We have simplified the process of customizing the context menu and this will be available in our October 2009 (Volume 4 ) release of Essential Diagram WPF. In this release, you will be able to specify the context menu for the node by using the ContextMenu property of the node (without the need to override Mouserightbuttonup event or ContextMenuOpening events ) as follows :

ContextMenu menu = new ContextMenu();
MenuItem m1 = new MenuItem();
m1.Header = "item1";
MenuItem m2 = new MenuItem();
m2.Header = "item2";
MenuItem m3 = new MenuItem();
m3.Header = "item3";

menu.Items.Add(m1);
menu.Items.Add(m2);
menu.Items.Add(m3);

nodeobj.ContextMenu=menu;

Therefore it will not be required to override the Mouserightbuttonup event or ContextMenuOpening events .

Please let us know if there are any concerns.

Regards,
Nikhil
WPF Team




Loader.
Live Chat Icon For mobile
Up arrow icon