When the user right clicks the header or tabheader of a contentpane I'd like to show a context menu. One of the items on the context menu would be to edit the text of the header or tabheader. I created a datatemplate containing a text block and added a context menu to the text block but when I right clicked on the text block the context menu did not appear.
DataTemplate headerTemplate = new DataTemplate();
headerTemplate.DataType = typeof(ContentPane);
FrameworkElementFactory headerTextBlock = new FrameworkElementFactory(typeof(TextBlock));
headerTextBlock.SetValue(TextBlock.TextProperty, "My Text");
ContextMenu cm = new System.Windows.Controls.ContextMenu();MenuItem mi = new MenuItem();
mi.Header = "Edit Tab Name"
mi.Command = ...
cm.Items.Add(mi);
headerTextBlock.SetValue(TextBlock.ContextMenuProperty, cm);
headerTemplate.VisualTree = headerTextBlock;
headerTemplate.Seal();
e.NewPane.TabHeaderTemplate = headerTemplate;
The above did not work - the context menu did not appear when the text block was right clicked.
What's the best way to get context menu for the header area?
I'm not sure how you are creating the menu items but the xamDockManager defines a ResourceKey for the style for the MenuItem and another for a Separator in the menu. Those resource keys are exposed as static properties on the xamDockManager (MenuItemStyleKey and MenuItemSeparatorStyleKey respectively). So if you're creating the items in code then you would use the SetResourceReference method on the menu item - e.g. mi.SetResourceReference(StyleProperty, XamDockManager.MenuItemStyleKey).
I am adding the MenuItem properly but the styling is not consistent. Is there a way to reapply the styling for the MenuItems?
Thanks,
Jon
Krasmir was offering a solution that would best provide the ability to expose your menu items because it would be mixed in with our menu items so you wouldn't lose the existing menu items, you wouldn't have to worry about how the end user chose to show the menu and you wouldn't have to worry about suppressing/ignoring a contextmenu being shown as a result of a right click/shift-f10/etc within the content of the ContentPane. With regards to height evaluation if you are dealing with something like PreviewMouseRightButtonUp then you would probably be better off starting with the e.OriginalSource and walking up the logical/visual tree to see if you are within a particular element. e.g.
var dp = e.OriginalSource as DependencyObject;while (dp is ContentElement) dp = LogicalTreeHelper.GetParent(dp);while (dp != null){ if (dp is PaneTabItem) { // the event happened within the pane tab break; } dp = VisualTreeHelper.GetParent(dp);}
We have reached an impass. I ask about determining the height of the tabheader so I can use mouse positition in a right button event and you insist I should use OptionsMenuOpening. Fortunately I found a way to make a reasonable estimate of the tabheight based on the actualheight of the content I put into the tabheader so I'm using that. I do not wish any further assistance.
Hello,
I am just checking if you require any further assistance on the matter.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support