WPF ResourceDictionary和Binding

时间:2017-02-03 07:37:38

标签: css wpf resourcedictionary

我是WPF的初学者。我想像css / sass一样做。我想重用一些定义。

这里我想在元素(例如按钮)中重用颜色定义。如果我使用“staticresource”绑定,运行时会出现以下异常:

“PresentationFramework.dll中发生了'System.Windows.Markup.XamlParseException'类型的未处理异常 附加信息:“#FF8FA2AC”istkeigültigerWertfürdieEigenschaft“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>

如何在其他元素中使用预定义的定义?或者有什么不对。我想定义4种不同的按钮样式“Standard”,“IsFocused”,“IsDisabled”和“IsHero(background = mangenta2”。

1 个答案:

答案 0 :(得分:0)

您应将Background属性设置为{{3}}。如果将其设置为SolidColorBrush,则可以将此属性的Color属性设置为Color资源,如下所示:

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