How do I copy a WPF resource in xaml? - wpf

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?

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"];

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.

Related

Using Caliburn.Micro and MahApps in a WPF app. Confused # MaterialDesignInXaml Documentation to integrate it with MahApps

I understand what's happening with MahApps and Caliburn; I am trying to add MaterialDesign.Themes (or MaterialDesign.MahApps package) and color package. The following section from MaterialDesign Documentation makes no sense to me. What am I supposed to do with this? I tried a lot of different things but it's totally missing me.
Here's the link
<SolidColorBrush x:Key="HighlightBrush" Color="{DynamicResource Primary700}"/>
<SolidColorBrush x:Key="AccentColorBrush" Color="{DynamicResource Primary500}"/>
<SolidColorBrush x:Key="AccentColorBrush2" Color="{DynamicResource Primary400}"/>
<SolidColorBrush x:Key="AccentColorBrush3" Color="{DynamicResource Primary300}"/>
<SolidColorBrush x:Key="AccentColorBrush4" Color="{DynamicResource Primary200}"/>
<SolidColorBrush x:Key="WindowTitleColorBrush" Color="{DynamicResource Primary700}"/>
<SolidColorBrush x:Key="AccentSelectedColorBrush" Color="{DynamicResource Primary500Foreground}"/>
<LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5">
<GradientStop Color="{DynamicResource Primary700}" Offset="0"/>
<GradientStop Color="{DynamicResource Primary300}" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="CheckmarkFill" Color="{DynamicResource Primary500}"/>
<SolidColorBrush x:Key="RightArrowFill" Color="{DynamicResource Primary500}"/>
<SolidColorBrush x:Key="IdealForegroundColorBrush" Color="{DynamicResource Primary500Foreground}"/>
<SolidColorBrush x:Key="IdealForegroundDisabledBrush" Color="{DynamicResource Primary500}" Opacity="0.4"/>
It's the Primary### part of the code that's throwing me off. I tried to replace it with
HighlightColor => Primary700
AccentColor => Primary500
AccentColor2 => Primary400
AccentColor3 => Primary300
AccentColor4 => Primary200
IdealForegroundColor => Primary500Foreground
But even that does not help. If I'm supposed to go and replace those names in a particular Xml dictionary where do I find it? This is such a simple step that's wasted so much time trying to figure it out.
What this snippet is doing, is telling the MahApps controls to use your Material Design palette(which is constituted from a primary colour, with multiple hues, and a single accent colour).
The full example is here (from one of the demos in the Material Design source):
https://github.com/ButchersBoy/MaterialDesignInXamlToolkit/blob/master/MahMaterialDragablzMashUp/App.xaml
You don't need to replace anything. So long as you are using a Material Design palette, this just instructs MahApps controls to use that palette.

How to create an array of linear gradient brushes in xaml using static resource?

I am trying to create an array of gradient brushes in xaml but I would like to point each array element back to a static resource that is the gradient brush. So, far I've been unable to achieve this. Can someone provide guidance on how I might accomplish this?
I want to add them to an x:Array but point to the resource rather than having to re-define the brushes as I've had to do here. I use the brushes in several places in addition to an array of them so I only want to define the brushes once.
<LinearGradientBrush x:Key="OrangeLinearGradientBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFF7941D" />
<GradientStop Offset="1" Color="#FFF26522" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="RedLinearGradientBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#F26263" />
<GradientStop Offset="1" Color="#B80303" />
</LinearGradientBrush>
<x:Array x:Key="BrushArray" Type="LinearGradientBrush">
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFF7941D" />
<GradientStop Offset="1" Color="#FFF26522" />
</LinearGradientBrush>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#F26263" />
<GradientStop Offset="1" Color="#B80303" />
</LinearGradientBrush>
</x:Array>
You may use StaticResourceExtensions:
<x:Array x:Key="BrushArray" Type="LinearGradientBrush">
<StaticResourceExtension ResourceKey="OrangeLinearGradientBrush"/>
<StaticResourceExtension ResourceKey="RedLinearGradientBrush"/>
</x:Array>

Partial-filling of a rectangle on Silverlight

I am new in Silverlight. I need to fill a rectangle partially with a SolidColorBrush (like a Bar, I need the border). With GradientBrush, the change of color is not sharp.
I can add two rectangles for this, but there has to be a better way.
Any suggestion would be appreciated.
I am using Silverlight 4.0.
Thanks for reading.
-Rakib
What about using gradient brush with gradient stops very close to each other, something like this:
<LinearGradientBrush x:Key="_backgroundBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Yellow" Offset="0.65" />
<GradientStop Color="Blue" Offset="0.66" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>

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

Using a StaticResource SolidColorBrush to define the Gradient Stop Colors

I am creating some wpf resource dictionaries with all the styles for an application! I have a few LinearGradientBrushes, where the color is set directly in the LinearGradientBrush reference as GradientStops. However, I want to have a predefined set of colors that I can use a a reference for each GradientStop, so that changing the color scheme for the application is a matter of changing the values of the SolidColorBrushes:
<SolidColorBrush Color="#5A5A5A" x:Key="colorbrushMedium" />
<SolidColorBrush Color="#222222" x:Key="colorbrushDark" />
<LinearGradientBrush>
<GradientStop Color="{StaticResource colorbrushMedium}"/>
<GradientStop Color="{StaticResource colorbrushDark}" Offset="1"/>
</LinearGradientBrush>
With the code example above, I am getting the following error:
Cannot convert the value in attribute 'Color' to object of type 'System.Windows.Media.Color'. '#5A5A5A' is not a valid value for property 'Color'.
The line it refers to is the line where <GradientStop Color="{StaticResource colorbrushMedium}"/> is defined.
Any ideas?
Ok, I found the problem:
Using Color and not SolidColorBrush..
<Color x:Key="colorbrushMedium">#FF5A5A5A</Color>
<Color x:Key="colorbrushDark">#FF222222</Color>
<LinearGradientBrush>
<GradientStop Color="{StaticResource colorbrushMedium}"/>
<GradientStop Color="{StaticResource colorbrushDark}" Offset="1"/>
</LinearGradientBrush>
This seems to solve my problem!
Use Binding to reference the color both in SolidColorBrush and in LinearGradientBrush:
<SolidColorBrush x:Key="stop1" Color="#FF5A5A5A"/>
<SolidColorBrush x:Key="stop2" Color="#FF222222"/>
<LinearGradientBrush x:Key="gradient">
<GradientStop Color="{Binding Source={StaticResource stop1},Path=Color}" Offset="0"/>
<GradientStop Color="{Binding Source={StaticResource stop2},Path=Color}" Offset="1"/>
</LinearGradientBrush>

Resources