触发器无法正常工作?

时间:2011-11-01 22:09:15

标签: wpf xaml triggers styles

嗯,我想我在这里遗漏了一些东西..

我试图制作一个地铁风格的wpf主题,所以我从这开始:

<Color x:Key="PrimaryColor">#8CBF26</Color>
<Color x:Key="TextColor">White</Color>
<Color x:Key="BackgroundColor">Black</Color>

<SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource ResourceKey=PrimaryColor}" />
<SolidColorBrush x:Key="ForgroundBrush" Color="{StaticResource ResourceKey=TextColor}" />
<SolidColorBrush x:Key="BackgroundBrush" Color="{StaticResource ResourceKey=BackgroundColor}" />

<FontFamily x:Key="FontFamily">Segoe WP, Segoe UI, Lucida Sans Unicode, Verdana</FontFamily>

<Style TargetType="{x:Type Window}">
    <Setter Property="Background" Value="{StaticResource BackgroundBrush}" />
</Style>
<Style TargetType="{x:Type Run}">
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="{StaticResource FontFamily}" />
    <Setter Property="Foreground" Value="{StaticResource ForgroundBrush}" />
</Style>
<Style TargetType="{x:Type TextBlock}">
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="{StaticResource FontFamily}" />
    <Setter Property="Foreground" Value="{StaticResource ForgroundBrush}" />
</Style>

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{StaticResource BackgroundBrush}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ForgroundBrush}"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="Foreground" Value="{StaticResource ForgroundBrush}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
                    <ContentPresenter Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" 
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" Value="{StaticResource ForgroundBrush}" />
                        <Setter Property="Foreground" Value="{StaticResource BackgroundBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这对所有人都很有用,但触发器Foreground属性永远不会被修改,我的猜测是已经设置了TextBlock,所以它无法覆盖它的forecolor是否有解决此问题的方法?

由于

1 个答案:

答案 0 :(得分:2)

编辑: TextBlock样式应用于ControlTemplate的{​​{1}}以某种方式覆盖其自己的样式。不知道该怎么办。


对我有用,您是否在Button实例上设置了Foreground?如果是这样,则会因precedence覆盖触发器。