1

Well i guess im missing something here..

Any way im trying to make a metro style wpf theme, so i started with this:

<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>

Well this works great for all but the trigger the Foreground property is never modified, my guess is that styled the TextBlock already so it cant override its forecolor is there any workaround for this problem??

Thanks

H.B.
  • 166,899
  • 29
  • 327
  • 400
Peter
  • 37,042
  • 39
  • 142
  • 198

1 Answers1

2

Edit: The TextBlock style is applied inside the ControlTemplate of the Button somehow overriding its own style. Not sure what to do about it.


Works for me, did you set a Foreground on the Button instance? If so that would override the trigger due to precedence.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • in the style for the button i define both a Foreground and a Background... the style is applied but the Foreground color is not changed when i click the button... – Peter Nov 01 '11 at 22:29
  • @Petoj: Well, as i said, it does for me. I just copied all that into some resources, created a button and when pressed the background turns white and the foreground black. – H.B. Nov 01 '11 at 22:31
  • Im totally confused i guess ill try to create a dummy project and try it there! – Peter Nov 01 '11 at 22:38
  • Ok it worked there now i just have to figure out why it doesn't work in my main project... – Peter Nov 01 '11 at 22:40
  • @Petoj: Did you try it in "normal" resources, because if you place it in `Application.Resources` the style for `TextBlock` will interfere with the Button style. Do you really need it? – H.B. Nov 01 '11 at 22:43
  • i guess thats my problem, is there any other place to stick it if i want it to be global in the application?? – Peter Nov 01 '11 at 22:47
  • @Petoj: No, sadly not, the problem is that those styles are applied inside `ControlTemplates` as well. – H.B. Nov 01 '11 at 22:48
  • @Petoj: The default Aero theme does not set `Foreground` in the `TextBlock` style (maybe for this reason) but the `Window` style, you might want to consider doing that as well so it is inherited down throughout the window. – H.B. Nov 01 '11 at 22:55
  • Styling the window does not seem to have any effect at all, in visual studio the changes are applied but the changes aren't applied when run, no matter where i place the ResourceDictionary.. (only tried Foreground and Background) – Peter Nov 01 '11 at 23:22
  • @Petoj: Well, it no longer applies inside templates, so its area of effect is a lot smaller. – H.B. Nov 01 '11 at 23:24
  • Last problem seems to be discussed already http://stackoverflow.com/questions/4279773/wpf-window-style-not-being-applied – Peter Nov 02 '11 at 07:32