在另一个LinearGradientBrush中使用LinearGradientBrush?

7
我将尝试使用一个线性渐变刷来定义另一个线性渐变刷。但我不知道这是否有效,如果有效,我需要知道如何操作。
例如:
    <LinearGradientBrush x:Key="ComboBoxFocusBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFFDEEB3" Offset="0"/>
        <GradientStop Color="#FFFBF2CD" Offset="1"/>
        <GradientStop Color="#FFFCE48A" Offset="0.5"/>
        <GradientStop Color="#FFFBE388" Offset="0.75"/>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="FilterPopupTitleBrush" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#B45988" Offset="0.75"/>
        //Code here to use ComboBoxFocusBackgroundBrush
        <GradientStop Color="#990088" Offset="0.75"/>
    </LinearGradientBrush>

感谢您提前提供答案。

编辑: 为了使示例更加清晰,我想使用“ComboBoxFocusBackgroundBrush”作为“FilterPopupTitleBrush”的“模板”,这样我就可以在两个刷子中使用相同的颜色渐变,而不需要复制“<GradientStop...>”标签。


你想做什么不是很清楚... - Thomas Levesque
2个回答

10
您可以像这样在多个画笔之间共享渐变点列表:-
<GradientStopCollection x:Key="MyGradient">
    <GradientStop Color="#FFFDEEB3" Offset="0"/> 
    <GradientStop Color="#FFFBF2CD" Offset="1"/> 
    <GradientStop Color="#FFFCE48A" Offset="0.5"/> 
    <GradientStop Color="#FFFBE388" Offset="0.75"/> 
</LinearGradientBrush> 

<LinearGradientBrush x:Key="ComboBoxFocusBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0"
   GradientStops="StaticResource MyGradient}" /> 

<LinearGradientBrush x:Key="FilterPopupTitleBrush" EndPoint="0.5,1" StartPoint="0.5,0"
    GradientStops="{StaticResource MyGradient}" /> 

现在您可以改变EndPointStartPoint和其他属性来创建相同基本渐变的不同变体。

您甚至可以将相同的设置提供给RadialGradientBrush


1
当我试图在Silverlight 4 ResourceDictionary中定义GradientStopCollection时,出现“值不在预期范围内”的错误,有任何想法为什么会这样? - dain
同样的问题出现在Silverlight 5中。我先尝试了编程方式,然后用XAML标记语言尝试,结果还是一样的。 - xr280xr

3

分享另一种实现方式,您不需要创建单独的集合,也可以重复使用现有的画笔,例如:

<LinearGradientBrush x:Key="FilterPopupTitleBrush" GradientStops="{Binding GradientStops, Source={StaticResource ComboBoxFocusBackgroundBrush}}"/>

在创建自定义画笔时,基于现有画笔的方式将会非常有用,特别是当你想要扩展预定义主题(如Telerik主题)时,更改XAML不是一个好方法。


这是一个很棒的答案。 - Bartosz Wójtowicz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接