WPF:Material Design + dragablz tabItem标题样式

时间:2016-01-16 19:05:30

标签: c# wpf view material-design material-design-in-xaml

我正在使用MaterialDesign Toolkit和Dragablz在WPF中工作。 我在尝试设置TabablzControl样式时遇到了问题。 我已经拥有了Windows默认TabControlTabItem标题的样式,如图所示: http://i.imgur.com/2anl5rl.png

但是当我将默认的tabControl更改为TabablzControl时,它变为: http://i.imgur.com/bhaaMVy.png

这是window.resources:

    <Style x:Key="mdTabControl" TargetType="TabControl">
        <Setter Property="TextElement.Foreground" Value="{DynamicResource MaterialDesignBody}"/>
        <Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}"></Setter>
    </Style>
    <Style x:Key="mdTabHeader" TargetType="{x:Type TabItem}">
        <Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}"></Setter>
        <Setter Property="Foreground" Value="{DynamicResource MaterialDesignBody}"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border Name="Border"  Margin="1,0,1,0" CornerRadius="3 3 0 0">
                            <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center"
                                              HorizontalAlignment="Center"
                                              ContentSource="Header" Margin="10,2,10,2"
                                              RecognizesAccessKey="True">
                            </ContentPresenter>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Panel.ZIndex" Value="100" />
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource SecondaryAccentBrush}" />
                            <Setter Property="Foreground" Value="{StaticResource SecondaryAccentForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter Property="Panel.ZIndex" Value="100" />
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource PrimaryHueMidBrush}" />
                            <Setter Property="Foreground" Value="{StaticResource PrimaryHueMidForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource PrimaryHueDarkBrush}" />
                            <Setter Property="Foreground" Value="{StaticResource PrimaryHueDarkForegroundBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

mdTabControl样式的targetType更改为时出现错误: TargetType="dbz:TabablzControl"

我希望将我设置的样式保持为TabControl,但增加了TabablzControl

的功能

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:8)

首先要注意的是,这是一般的WPF特性,你没有正确使用样式继承。

当您使用带有Dragablz的Material Design时,如果要重新设置选项卡控件本身,则必须使用 BasedOn 继承Dragablz装配中的Material Design样式:

<Style x:Key="mdTabControl" TargetType="TabControl" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}"> 
    <Setter Property="TextElement.Foreground" Value="{DynamicResource MaterialDesignBody}"/>
    <Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}"></Setter>
</Style>

同样,使用选项卡标题本身,您需要继承相关的样式:

<Style x:Key="mdTabHeader" TargetType="{x:Type TabItem}" BasedOn="{StaticResource MaterialDesignDragableTabItemStyle}">
    . . .
</Style>

注意,(根据您的App.xaml设置),您可能需要确保在同一个XAML文件中包含正确的资源字典。例如,更完整的XAML可能是:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Dragablz;component/Themes/materialdesign.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <Style x:Key="NormalTabItemStyle" TargetType="{x:Type dragablz:DragablzItem}" BasedOn="{StaticResource MaterialDesignDragableTabItemStyle}">
            <Setter Property="Width" Value="280" />
            <Setter Property="Padding" Value="1" />
        </Style>
        . . .
    </ResourceDictionary>                
</Window.Resources>

最后,当您更改TabItem样式时,您需要使用TabablzCOntrol样式正确的样式,或者您可以在实际声明TabablzControl本身的地方使用它:

<dragablz:TabablzControl ItemContainerStyle="{StaticResource mdTabHeader}" />

所有这一切的一个很好的例子是演示解决方案中的 SidePanels 项目:https://github.com/ButchersBoy/DragablzSamplez

答案 1 :(得分:0)

   <Style TargetType="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}"/>