我如何从Windows主题继承/覆盖强制样式?

时间:2008-11-27 09:10:45

标签: wpf inheritance themes styles

目前我正在强迫我的WPF应用使用luna主题,无论如何,使用此XAML代码

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles.xaml" />
            <ResourceDictionary Source="NavigationCommands.xaml" />
            <ResourceDictionary Source="/RibbonControlsLibrary;component/Themes/Office2007Blue.xaml"/>
            <ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;;component/Themes/luna.normalcolor.xaml" /> 
            </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

现在我想用这个验证触发器扩展每个文本框的样式

<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="Background" Value="#d3e1f3"></Setter>
            <Setter Property="ToolTip"
            Value="{Binding RelativeSource={RelativeSource Self}, 
                   Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

但是这个触发器不起作用,因为我强迫了luna主题。 (没有强制主题,每个东西都应该工作,但看起来不应该:() 有没有办法强制卢娜主题和扩展它的风格?可能在BasedOn属性上?

atm我为相关风格定义了一个键,并将其添加到每个文本框中,这样可行,但不是最漂亮的方式。

TIA

4 个答案:

答案 0 :(得分:1)

尝试

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">

答案 1 :(得分:1)

类型样式的BasedOn语法如下:

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

HTH

答案 2 :(得分:0)

您是否尝试过首先设置lune resourcedictionary,然后设置自己的resourcedictionary? 我可以想象luna主题会覆盖你的风格。

<Application.Resources>
   <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;;component/Themes/luna.normalcolor.xaml" />
            <ResourceDictionary Source="/RibbonControlsLibrary;component/Themes/Office2007Blue.xaml"/> 
            <ResourceDictionary Source="Styles.xaml" />
            <ResourceDictionary Source="NavigationCommands.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

答案 3 :(得分:0)

使用上面提到的

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

但也请确保您的词典包含在正确的顺序中 - 首先是基于您的风格的词典

<Application.Resources>
<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/RibbonControlsLibrary;component/Themes/Office2007Blue.xaml"/>
        <ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;;component/Themes/luna.normalcolor.xaml" /> 

        <ResourceDictionary Source="Styles.xaml" />
        <ResourceDictionary Source="NavigationCommands.xaml" />
   </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>