0

I am trying to create a label style that applies a foreground color defined in a brushes resource directory.

<Color x:Key="TextForegroundColor" >#8B4513</Color>

<SolidColorBrush 
    x:Key="TextForegroundColorBrush"
    Color="{Binding Source={StaticResource TextForegroundColor}, Path=Color}" />
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="MVILabelStyle" TargetType="{x:Type Label}">
    <Setter Property="HorizontalAlignment" Value="Right" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Foreground" Value="{StaticResource TextForegroundColorBrush}" />
</Style>

When I run the app, the foreground is not applied, but all of the others are. What am I doing wrong?enter image description here

John Baird
  • 2,606
  • 1
  • 14
  • 33

2 Answers2

0

The Binding

Color="{Binding Source={StaticResource TextForegroundColor}, Path=Color}"

is wrong. It would require that a Color has a Color property.

You would either write

<SolidColorBrush Color="{Binding Source={StaticResource TextForegroundColor}}"/>

or just

<SolidColorBrush Color="{StaticResource TextForegroundColor}"/>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thanks for replying. I used what you said and now have : , but still no color in the actual label. – John Baird Aug 23 '20 at 15:03
  • No idea, are the SolidColorBrush and the Style declared in different ResourceDictionaries? – Clemens Aug 23 '20 at 16:49
  • Add the former to the MergedDictionaries of the latter, e.g. like here: https://stackoverflow.com/q/45687750/1136211 – Clemens Aug 23 '20 at 17:28
0

Okay, after three days of searching, removing things, I finally found one control that applied the styles and one that didn't. In going step by step through each comparing things, I found a statement in the codebehind that disabled the control. That's what was going on. It was disabled and there fore was showing the correct style. I enabled the control, and bingo, styles applied. Thanks to all who participated.

John Baird
  • 2,606
  • 1
  • 14
  • 33