Witf tabitem header context menu

How to add a context menu to witf tabitem that appears only when you click on the tabitem title, and not on the content? I also need to dynamically create tabitems in .cs, so this will not work statically in .xaml.

I tried adding a context menu to tabitem.header, but it has some problems if I have [tabitem 1] [tabitem2] [Tabitemtabitemtabitemta]

[tabitem2] stretches to fit the width of the tabcontrol. Any help would be appreciated.

Thanks!

+4
source share
1 answer

See this question on how to do this programmatically. The trick is to install ContextMenu on any control that you set as the contents of the header. If you just use the header to set a simple string value, this will not work. At a minimum, you need to create a TextBlock or ContentControl or something like that.


For those who are interested in how to do this using XAML (especially when using the MVVM template):

Set ContextMenu in the ItemContainerStyle TabControl. It will only apply to the actual tab (title), and not to the contents of the tab. You can use bindings, etc. In MenuItems to get a variety of tab-based behaviors if your tab uses ViewModel ..

<TabControl> <TabControl.ItemContainerStyle> <Style TargetType="{x:Type TabItem}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu/> <!-- Define it here! --> </Setter.Value> </Setter> </Style> </TabControl.ItemContainerStyle> </TabControl> 
+6
source

All Articles