自定义控件不使用主题

时间:2014-09-15 21:53:09

标签: wpf styles themes custom-controls basedon

我的应用程序设置我们使用皇家主题:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component\themes\Royale.NormalColor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后我在同一个应用程序项目中有一个CustomControl:

public class CustomerView : Control
{
    static CustomerView()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomerView), 
            new FrameworkPropertyMetadata(typeof(CustomerView)));
    }
}

 <Style TargetType="{x:Type local:CustomerView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomerView}">
                <TabControl>
                    <TabItem Header="Tab0" />
                    <TabItem Header="Tab1" />
                    <TabItem Header="Tab2" />
                </TabControl>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是有效的,皇家主题的样式应用于Tabcontrol。但现在我想对标签项上的填充进行一些改动:

<Style TargetType="{x:Type local:CustomerView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomerView}">
                <TabControl>
                    <TabControl.ItemContainerStyle>
                        <Style TargetType="{x:Type TabItem}"
                               BasedOn="{StaticResource {x:Type TabItem}}">
                            <Setter Property="Padding"
                                    Value="20" />
                        </Style>
                    </TabControl.ItemContainerStyle>

                    <TabItem Header="Tab0" />
                    <TabItem Header="Tab1" />
                    <TabItem Header="Tab2" />
                </TabControl>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在皇家主题不适用于TabControl并返回到Aero主题。是什么赋予了?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我不确定为什么设置ItemContainerStyle会覆盖样式,但是如果你只是将它作为资源添加到ControlTemplate它将按预期工作

<Style TargetType="{x:Type local:CustomerView}">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:CustomerView}">
            <ControlTemplate.Resources>
                <Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
                     <Setter Property="Padding" Value="20" />
                </Style>
            </ControlTemplate.Resources>
            <TabControl>
                <TabItem Header="Tab0" />
                <TabItem Header="Tab1" />
                <TabItem Header="Tab2" />
            </TabControl>
        </ControlTemplate>
    </Setter.Value>
</Setter>

希望这能解决你的问题。