wpf 使用 StaticResource SolidColorBrush 定义渐变停止颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1831977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:17:34  来源:igfitidea点击:

Using a StaticResource SolidColorBrush to define the Gradient Stop Colors

wpfxamlresourcescolorsbrushes

提问by code-zoop

I am creating some wpf resource dictionaries with all the styles for an application! I have a few LinearGradientBrushes, where the color is set directly in the LinearGradientBrushreference as GradientStops. However, I want to have a predefined set of colors that I can use a a reference for each GradientStop, so that changing the color scheme for the application is a matter of changing the values of the SolidColorBrushes:

我正在创建一些 wpf 资源字典,其中包含应用程序的所有样式!我有几个LinearGradientBrushes,其中颜色直接在LinearGradientBrush参考中设置为GradientStops。但是,我想要一组预定义的颜色,我可以为每个颜色使用参考GradientStop,这样更改应用程序的配色方案就是更改SolidColorBrushes的值的问题:

<SolidColorBrush Color="#5A5A5A" x:Key="colorbrushMedium" /> 
<SolidColorBrush Color="#222222" x:Key="colorbrushDark" />  


<LinearGradientBrush>
    <GradientStop Color="{StaticResource colorbrushMedium}"/>
    <GradientStop Color="{StaticResource colorbrushDark}" Offset="1"/>
</LinearGradientBrush>

With the code example above, I am getting the following error:

使用上面的代码示例,我收到以下错误:

Cannot convert the value in attribute 'Color' to object of type 'System.Windows.Media.Color'. '#5A5A5A' is not a valid value for property 'Color'.  

The line it refers to is the line where <GradientStop Color="{StaticResource colorbrushMedium}"/>is defined.

它所指的行<GradientStop Color="{StaticResource colorbrushMedium}"/>是定义所在的行。

Any ideas?

有任何想法吗?

回答by code-zoop

Ok, I found the problem:

好的,我发现了问题:

Using Color and not SolidColorBrush..

使用 Color 而不是 SolidColorBrush ..

<Color x:Key="colorbrushMedium">#FF5A5A5A</Color>
<Color x:Key="colorbrushDark">#FF222222</Color>
<LinearGradientBrush>
    <GradientStop Color="{StaticResource colorbrushMedium}"/>
    <GradientStop Color="{StaticResource colorbrushDark}" Offset="1"/>
</LinearGradientBrush>

This seems to solve my problem!

这似乎解决了我的问题!

回答by sunny moon

Use Bindingto reference the color both in SolidColorBrushand in LinearGradientBrush:

使用Binding引用的颜色都在SolidColorBrushLinearGradientBrush

<SolidColorBrush x:Key="stop1" Color="#FF5A5A5A"/>
<SolidColorBrush x:Key="stop2" Color="#FF222222"/>

<LinearGradientBrush x:Key="gradient">
  <GradientStop Color="{Binding Source={StaticResource stop1},Path=Color}" Offset="0"/>
  <GradientStop Color="{Binding Source={StaticResource stop2},Path=Color}" Offset="1"/>
</LinearGradientBrush>