在我的应用程序中,我最近尝试使其外观更加生动,因此我尝试使用 MahApps.Metro
。但我遇到了一些障碍。
在我的应用程序中,我有一个 tabcontrol
并且我使用了 style
,如下所示;
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--Resource dictionary for mahapps. -->
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type TabItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- FormName is the name of the ViewModel-->
<TextBlock Text="{Binding FormName}" VerticalAlignment="Center" Margin="2" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsValid}"
Value="False">
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Page.Resources>
<!-- The itemsource that is bound to is a ObservableCollection of Forms that are used to validate for a Progress bar, It uses the ViewModels-->
<TabControl x:Name="tabcontrol"
Grid.Row="1"
ItemsSource="{Binding Forms}"
SelectedIndex="0" BorderBrush="Black" />
显然,这没有使用 MahApps.Metro
tabcontrol
。但是,在 tabcontrol
的 style
中,我将 TargetType
更改为以下内容,但它导致 Page
中的所有内容增加其大小并将两个选项卡内容有效地合并为一个;
<Style TargetType="{x:Type Controls:MetroTabItem}" BasedOn="{StaticResource MetroTabItem}">
如果能帮助我实现 MahApps.Meto
选项卡控件
并纠正我做错的事情,我们将不胜感激。干杯。
最佳答案
您得到所有混合结果的原因是因为 MetroTabItem
没有自己的 Template
我认为并使用 TabItem
> 改为覆盖样式。您原来的方法是正确的。
我无法告诉您的实际样式是如何配置的(您粘贴到此处的 ResourceDictionary
的剪切粘贴将无法按原样进行编译),但是如果您使用此资源 block (并且添加你的),它应该可以工作。在这里进行测试并工作。
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary>
<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FormName}" VerticalAlignment="Center" Margin="2" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsValid}" Value="False" />
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
注意:VS 会提示“在属性表达式中检测到循环”。这是正确的,但效果很好。您可以忽略该错误。该解决方案将构建并正常运行。
关于c# - 无法设置 TabControl 的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16564047/