Use a LinearGradientBrush in another LinearGradientBrush?
Asked Answered
E

2

7

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

Exhibitive answered 14/9, 2010 at 12:24 Comment(1)
It's not very clear what you want to do...Hooked
J
10

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.

Jovia answered 14/9, 2010 at 12:59 Comment(2)
When I'm trying to define a GradientStopCollection in a Silverlight 4 ResourceDictionary I get a "Value does not fall within the expected range" error, any idea why?Grantland
Same here with Silverlight 5. Found it programmatically first and then tried with this XAML markup and got the same.Prasad
W
3

Sharing another way of doing this, you don't need to create a separate collection, you can also reuse the existing brush like

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

This way of creating a custom brush based on existing brush will be helpful specially when you want to extend predefined themes like Telerik themes, where it would not be good approach to change the XAML from telerik.

Waterish answered 24/8, 2012 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.