无法设置TabControl样式

时间:2013-05-15 11:39:24

标签: 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。但是,在style的{​​{1}}内,我将tabcontrol更改为以下内容,但它会导致TargetType内的所有内容增加其大小,并将两个标签内容合并为一个;

Page

enter image description here

在帮助我实施<Style TargetType="{x:Type Controls:MetroTabItem}" BasedOn="{StaticResource MetroTabItem}"> MahApps.Meto并清理我做错的事情方面,任何帮助都会很感激。欢呼声。

2 个答案:

答案 0 :(得分:3)

你得到所有混淆结果的原因是因为我认为MetroTabItem没有自己的Template,而是使用TabItem样式覆盖。你原来的方法是在正确的道路上。

我无法分辨你的实际样式是如何配置的(你在这里粘贴的ResourceDictionary剪切粘贴不会按原样编译)但是如果你使用这块资源(并添加你的) ,它应该工作。在这里测试和工作。

<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会抱怨“在属性表达式中检测到循环”。这是真的,但工作正常。您可以忽略该错误。该解决方案将构建并运行良好。

答案 1 :(得分:0)

基于Maverik上面所说的内容(不仅仅是添加评论的声誉......)尝试使用BasedOn =&#34; {StaticResources MetroTabItem}&#34;防止循环投诉。

<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 MetroTabItem}">
            <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>