Double clicks and context clicks

A simple mouse click on a selectable Part will result in that part becoming selected. This is the behavior of the ClickSelectingTool.

If you want to perform some custom action when the user double-clicks on a part, you can define an event handler for the part:

<DataTemplate x:Key="NodeTemplate4"> <Border . . . MouseLeftButtonDown="Node_MouseLeftButtonDown"> . . . </Border> </DataTemplate>

private void Node_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (DiagramPanel.IsDoubleClick(e)) { Node node = Part.FindAncestor<Node>(sender as UIElement); if (node != null && node.Data != null) { e.Handled = true; MessageBox.Show("double clicked on " + node.Data.ToString()); } } }

You can implement context menus for nodes easily enough by just defining them in your node template:

<DataTemplate x:Key="NodeTemplate3"> <Border . . .> <ContextMenuService.ContextMenu> <ContextMenu> <MenuItem Header="some node command" Click="MenuItem_Click" /> </ContextMenu> </ContextMenuService.ContextMenu> </Border> </DataTemplate>

<span style=“font-size: 11pt; line-height: 115%; font-family: “Calibri”,“sans-serif”;”>private void MenuItem_Click(object sender, RoutedEventArgs e) { var partdata = ((FrameworkElement)sender).DataContext as PartManager.PartBinding; if (partdata == null || partdata.Data == null) { MessageBox.Show("Clicked on nothing"); } else { MessageBox.Show("Clicked on data: " + partdata.Data.ToString()); } }
You can also get the “partdata.Node” if you want access to the Node rather than the data to which the node is bound.

Note that Silverlight 3 does not support
receiving right mouse click events, so you cannot easily implement normal
context menus in Silverlight 3. This will not be a restriction in Silverlight 4.

I also have a tooltip defined for the node and the context menu appears when I right click the node second time. It shows the tooltip the first time. Any solutions? Also how can I show a context menu on the entire diagram?

I believe you’re talking about Silverlight 4, right?

Context menus on nodes or links work even in the presence of tooltips, as far as I can tell. In other words, if you add a ToolTipService.ToolTip and a ContextMenuService.ContextMenu to the Border of a node DataTemplate, the user sees the tooltip and is able to single-right-click to get the ContextMenu.

I’ll investigate defining default context menus for the whole diagram.

I have updated the original post to include the definition of a simple MenuItem_Click event handler, to show how to get from the MenuItem to the Part.Data that the user context-clicked on.

For version 1.2 we have added a Diagram.ContextMenuEnabled property, to make it easier to define a default ContextMenu for a Diagram.

The default value is false. It does not affect context menus defined in DataTemplates for Nodes or for Links.