views:

826

answers:

2

I want to assign a resource I already have a second name, similar to using the BasedOn property of Styles. Specifically I have a brush that I use for a group of elements called ForegroundColor and I would like to use it in a control template (a ComboBox) calling it MouseOverBackgroundBrush. I would like to do something like this:

<ResourceCopy x:key="MouseOverBackgroundBrush" Value="{StaticResource ForegroundColor}" />

Is there a way to do this or is there a better way to go about this in Xaml?

+2  A: 

This is a feature that doesn't have very good support in XAML. I believe that you'll either need to repeat yourself (and change both locations anytime you need to change the brush) or if you don't mind a bit of code behind, you can accomplish the duplication like this:

Resources["MouseOverBackgroundBrush"] = Resources["ForegroundColor"];
David Mitchell
The problem with the code behind is they are both in resource dictionaries so there is no code attached to the resources.
Bryan Anderson
Actually you can have code behind for ResourceDictionaries. You need to use x:Class.
Todd White
@Todd - Good idea, though if I need to add the extra file anyway I'd probably just make a custom user control and assign the brush to a dependency property since I think that would be a little more readable/maintainable. Thoughts?
Bryan Anderson
My thoughts would be to just use the same static resource and name it so that it is reusable. When the resources actually need to be different then create the new static resource.
Todd White
+1  A: 

I don't know about how to copy a resource in xaml (can it even be done?) like you are asking ... but here is one way to accomplish what you are trying to do:

<Color x:Key="firstColor">#FFD97A7A</Color>
<Color x:Key="secondColor">#FFF4BFBF</Color>
<LinearGradientBrush x:Key="firstGradientBrush" EndPoint="0.5,1" StartPoint="0.5,0">
 <GradientStop Color="{DynamicResource firstColor}" Offset="0"/>
 <GradientStop Color="{DynamicResource secondColor}" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="secondGradientBrush" EndPoint="0.5,1" StartPoint="0.5,0">
 <GradientStop Color="{DynamicResource firstColor}" Offset="0"/>
 <GradientStop Color="{DynamicResource secondColor}" Offset="1"/>
</LinearGradientBrush>

Basically, create two different brushes based on some common colors.

cplotts
I would like to be able to change brushes in the future and have the update propagate through the rest of my UI. For instance if I have a ControlBackground brush which is a SolidColorBrush I'm going to want to make it a LinearGradientBrush at some point without making the change in 30 places.
Bryan Anderson
An easier way (imo) to do you answer is: <SolidColorBrush x:Key="MouseOverBrush" Color="{Binding Source={StaticResource ForegroundColor}, Path=Color}" />
Bryan Anderson
Yes, unfortunately, if you are going to change brush types ... then this method wouldn't work.
cplotts
Why do you think it easier to data bind to a Color ... than to use DynamicResource or StaticResource? Do you mean that you find it more readable or something? More performant? Most xaml that I've seen .. has gone the DynamicResource or StaticResource route.
cplotts
Basically just fewer tags, you don't have to look somewhere else to figure out what colors/options are part of the brush for the one you inherit options from. Really it's just a stylistic thing for me.
Bryan Anderson