Normally, when I set Header of the TabItem to some string, MahApps styles are applied to the TabItem Header.
However, I programmatically created custom TabItem, it's header is not a string but a StackPanel and all styling from MahApps is lost. How can I re-apply MahApps styles to my custom TabItem Header?
TabItem tabItem = new TabItem();
StackPanel tabItemHeader = new StackPanel();
tabItemHeader.Orientation = Orientation.Horizontal;
var tabTitle = new Label() {Content = "Userstory"};
var closeButton = new Button() {Content = "x"};
tabItemHeader.Children.Add(tabTitle);
tabItemHeader.Children.Add(closeButton);
tabItem.Header = headerStackPanel;
You should do this with the power of XAML and styles. Here is a short sample what you can do:
Put this style to Window
resources or App
resources.
<Style x:Key="CustomTabItemStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource MetroTabItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{TemplateBinding Content}" />
<Button Margin="2" VerticalAlignment="Top" Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}" Content="x" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
and use it at your TabItems
<TabItem Header="Userstory" Style="{StaticResource CustomTabItemStyle}">
<!-- your tab content -->
</TabItem>
Benefit of using styles is, that you can do/change what you want...
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.