Search code examples
wpfsilverlightxamlcolorscolor-space

Use a LinearGradientBrush in another LinearGradientBrush?


I'm trying to use one LinearGradientBrush in the Definition of another LinearGradientBrush. But I've no idea weather this would even work, and if it works, I need to know how.

For Example:

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

thanking you in anticipation for your answers

Edit: To get things a bit more clear in the example I want to use "ComboBoxFocusBackgroundBrush" in the "FilterPopupTitleBrush" as a "template".So that I've the same color gradient in both brushes without a copy of the "<GradientStop...>"-tags


Solution

  • You can share the list of gradient stops between multiple brushes, like this:-

    <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}" /> 
    

    Now you can vary the EndPoint, StartPoint and other properties create different variants of the same basic gradient.

    You can even supply the same set to RadialGradientBrush.