0

I#m a beginner with WPF. I want to make the same like css/sass. I want to reuse some definitions.

Here I want to reuse a color definition in an element (e.g. button). If I use binding with "staticresource", I get the following exception when running:

"An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: "#FF8FA2AC" ist kein gültiger Wert für die Eigenschaft "Background"."

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    >

    <Color x:Key="cyan3">#007ca9</Color>
    <Color x:Key="mangenta2">#a8005c</Color>
    <Color x:Key="wintergrey1">#e6ecf0</Color>
    <Color x:Key="wintergrey2">#c3ced5</Color>
    <Color x:Key="wintergrey3">#8fa2ac</Color>
    <Color x:Key="wintergrey4">#506671</Color>
    <Color x:Key="white">#FFFFFF</Color>
    <Color x:Key="antrazit">#333333</Color>

    <!-- Base style for button -->
    <Style TargetType="Button" x:Key="btnStandard">
        <!--Setter Property="Background" Value="#8fa2ac"/-->
        <Setter Property="Background" Value="{StaticResource wintergrey3}"/>
        <Setter Property="Foreground" Value="#ffffff"/>
        <Setter Property="Width" Value="150" />
        <Setter Property="Height" Value="30"/>
    </Style>
</ResourceDictionary>

How can I use predefined definitions in other elements? Or what is wrong. I want do define 4 different button styles "Standard", "IsFocused", "IsDisabled" and "IsHero (background=mangenta2".

Holgis
  • 1
  • 1
  • `Background` is not a color, it is a `Brush`. declare brushes in resources (e.g. SolidColorBrush) and reuse them in your styles and views; btw, `` creates a Brush from "#8fa2ac" hex code – ASh Feb 03 '17 at 07:39
  • Possible duplicate of [How do I convert a Color to a Brush in XAML?](http://stackoverflow.com/questions/3309709/how-do-i-convert-a-color-to-a-brush-in-xaml) – MikeT Feb 03 '17 at 10:38
  • I have change `#007ca9` to this `#007ca9` – live2 Jan 20 '22 at 20:24

1 Answers1

0

You should set the Background property to a Brush. If you set it to a SolidColorBrush you can then set the Color property of this one to your Color resource like this:

<Style TargetType="Button" x:Key="btnStandard">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="{StaticResource wintergrey3}" />
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="#ffffff"/>
    <Setter Property="Width" Value="150" />
    <Setter Property="Height" Value="30"/>
</Style>
mm8
  • 163,881
  • 10
  • 57
  • 88