我是 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/