css - WPF ResourceDictionary 和绑定(bind)

标签 css wpf resourcedictionary

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

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

“PresentationFramework.dll 中发生类型为‘System.Windows.Markup.XamlParseException’的未处理异常 附加信息:“#FF8FA2AC”ist kein gültiger Wert für die Eigenschaft“背景”。

<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)”。

最佳答案

您应该将背景属性设置为 Brush .如果将其设置为 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>

关于css - WPF ResourceDictionary 和绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42019073/

相关文章:

c# - 自定义 ItemsControl 和选择支持

wpf - 如何向用户控件添加更多资源

silverlight - 在运行时添加 ResourceDictonary 时出现异常

javascript - 动态设置 Button 的高度为 Textarea 的高度

html - 如何使用 CSS3 实现类似 Facebook 的不透明效果?

WPF - 在 StackPanel 中设置 Horizo​​ntalAlignment= 拉伸(stretch)到文本框

c# - WCF 到 WPF - 数据契约到模型

c# - 合并字典中的 WPF 覆盖样式

jquery 仅在 Safari 中产生 margin 问题

CSS 选择直接子项,但如果在另一个嵌套子项中则不会