Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to point one resource (a SolidColorBrush) at another

I set up a load of SolidColorBrush and LinearGradientBrush resources in a ResourceDictionary. I used these as I was restyling several controls for use in our application.

Now I have a bunch of other external brushes that I have to use for a variation on our app. These are also set up in a ReseourceDictionary.

Is it possible to point my brush resources at the new resources in another dictionary, something similar to the "BasedOn" attribute of Styles?

Something like this, conceptually at least:

<SolidColorBrush x:Key="MyDataGridHeaderBrush" Binding="HeaderBrushDefinedElsewhere"/>

...or is this kind of thing not possible, in which case I have to simply go do a Find/Replace and replace all my brush names with the new brush names?

Thanks in advance,

AT

like image 514
Andy T Avatar asked Mar 30 '11 13:03

Andy T


2 Answers

I agree with what Rachel said, but if you have to base it on an existing SolidColorBrush, you can do it with the following:

<SolidColorBrush x:Key="MyDataGridHeaderBrush" 
                 Color="{Binding Source={StaticResource HeaderBrushDefinedElsewhere}, Path=Color}"/>

Note this just works for the "Color" attribute, you'd have to do it separately for each attribute you needed.

like image 90
markmuetz Avatar answered Nov 11 '22 04:11

markmuetz


Usually I do a static Color property in one place, and have my brushes bind to that Color.

<SolidColorBrush x:Key="LightColor" Color="#C5DBF6"/>
<SolidColorBrush x:Key="DarkColor" Color="#FF8DB2E3"/>

<LinearGradientBrush x:Key="FadeOutRight" EndPoint="1,1" StartPoint="0,0">
        <GradientStop Color="{Binding Source={StaticResource LightColor}, Path=Color}" Offset="0" />
        <GradientStop Color="{Binding Source={StaticResource DarkColor}, Path=Color}" Offset="1"/>
</LinearGradientBrush>

You can also bind other SolidBrushColors to this:

<SolidColorBrush Color="{Binding Source={StaticResource LightColor}, Path=Color}" />

If this is referenced in another file, it might underline it because it can't find the static resource, but at runtime it will still compile providing your main resource file containing your brush definitions is loaded.

like image 32
Rachel Avatar answered Nov 11 '22 04:11

Rachel