[英]How can I apply MahApps style to my own WPF control?
通常,当我将TabItem的Header设置为某些字符串时,MahApps样式将应用于TabItem Header。
但是,我以编程方式创建了自定义TabItem,它的标头不是字符串,而是StackPanel,MahApps的所有样式都丢失了。 如何将MahApps样式重新应用到自定义TabItem标头?
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;
您应该利用XAML和样式的力量来做到这一点。 这是您可以做什么的简短示例:
将此样式放在Window
资源或App
资源中。
<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>
并在你的TabItems上使用它
<TabItem Header="Userstory" Style="{StaticResource CustomTabItemStyle}">
<!-- your tab content -->
</TabItem>
使用样式的好处是,您可以执行/更改所需的操作...
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.