c# - 无法设置 TabControl 的样式

标签 c# wpf xaml styles mahapps.metro

在我的应用程序中,我最近尝试使其外观更加生动,因此我尝试使用 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。但是,在 tabcontrolstyle 中,我将 TargetType 更改为以下内容,但它导致 Page 中的所有内容增加其大小并将两个选项卡内容有效地合并为一个;

<Style TargetType="{x:Type Controls:MetroTabItem}"  BasedOn="{StaticResource MetroTabItem}">

enter image description here

如果能帮助我实现 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/

相关文章:

.net - 在数据网格中水平滚动时绑定(bind)警告

c# - 如何在作为 List<string> 的 ObservableCollection 绑定(bind)属性(到 ListView)上调用 String.Join

xaml - 如何使用 WPF 设计复杂表格

c# - 更改 ListView 中选定行的颜色

c# 是否可以在 Windows 窗体应用程序中显示实时网页?

c# - 将 JSON 字符串解析为 .NET 对象

c# - Monotouch 事件

c# - MYSQL 空值干扰 C# 应用程序

c# - 该组件没有由 uri 标识的资源

c# - 使用c#检查mysql数据库中是否有记录