get value for a resource from another resource in XAML - wpf

I want SystemColors.ControlBrushKey to have same color as SystemColors.HighlightBrushKey. How do I specify it in XAML? I've tried:
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{StaticResource SystemColors.HighlightBrushKey}"/>
But it throws an exception: "Cannot find resource named 'SystemColors.HighlightBrushKey'. Resource names are case sensitive.".
Then I've tried:
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{StaticResource {x:Static Member=SystemColors.HighlightBrushKey}}"/>
And the exception is:
'#FF3399FF' is not a valid value for property 'Color'.
After that I've tried:
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static Member=SystemColors.HighlightBrushKey}"/>
And I've also got an error: "'HighlightBrush' is not a valid value for property 'Color'."
So how do I specify it correctly?

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"/>

Related

How to make multiple SolidColorBrush refer to one color in ResourceDictionary?

I have a "ResourceDictionary1.xaml" file to control colors in "MainWindow.xaml" file (intended for skinning) and it is working fine.
To have flexibility over colors, i defined separate SolidColorBrush resource for each individual control. But most of the time multiple SolidColorBrush use same color for clean look.
Now when i want to change the color i need to change it in all SolidColorBrush resources. So i want to bind all the SolidColorBrush color to one Color resource. When i need full control i can just remove the binding and specify another color.
So i created color resource in ResourceDictionary file. But when trying to bind the color the Key does not show up in intelliSense of VisualStudio.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFWindow.Skins"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Color x:Key="DarkH">#444444</Color>
<Color x:Key="DarkM">#555555</Color>
<Color x:Key="DarkL">#666666</Color>
<sys:String x:Key="TitleName">My App</sys:String>
<sys:Double x:Key="TitleBarHeight">32</sys:Double>
<SolidColorBrush x:Key="CicleMOut" Color="#aaaaaa"/>
<SolidColorBrush x:Key="CicleMOver" Color="#dddddd"/>
<Thickness x:Key="CircleMargin">8</Thickness>
<SolidColorBrush x:Key="TitleBarMOut" Color="#555555"/>
<SolidColorBrush x:Key="TitleBarMOver" Color="#555555"/>
<SolidColorBrush x:Key="ExitMOut" Color="#555555"/>
<SolidColorBrush x:Key="ExitMOver" Color="#666666"/>
<SolidColorBrush x:Key="ExitCross" Color="#aaaaaa"/>
<Thickness x:Key="CrossMargin">12</Thickness>
<sys:Double x:Key="CrossThickness">2</sys:Double>
<SolidColorBrush x:Key="MaxiMOut" Color="#555555"/>
<SolidColorBrush x:Key="MaxiMOver" Color="#666666"/>
<SolidColorBrush x:Key="MaxiBox" Color="#aaaaaa"/>
<Thickness x:Key="BoxMargin">12</Thickness>
<sys:Double x:Key="BoxThickness">1</sys:Double>
<SolidColorBrush x:Key="MiniMOut" Color="#555555"/>
<SolidColorBrush x:Key="MiniMOver" Color="#666666"/>
<SolidColorBrush x:Key="MiniBar" Color="#aaaaaa"/>
<Thickness x:Key="BarMargin">12</Thickness>
<sys:Double x:Key="BarThickness">1</sys:Double>
<SolidColorBrush x:Key="TitleBrs" Color="#aaaaaa"/>
<sys:Double x:Key="TitleFontSize">14</sys:Double>
<SolidColorBrush x:Key="FrmBdy" Color="#666666" />
Say i want to bind SolidColorBrush color to DarkH color.
<SolidColorBrush x:Key="FrmBdy" Color="{Binding DarkH}" />
This doesn't work. I got to know, binding in this way is not possible.
Then is there any other way to make multiple brushes refer to one color?
Because when there are say 20 or more SolidColorBrush, changing colors in each brush to same color doesn't sound efficient and cannot use single SolidColorBrush for above said flexibility reason.
I am new to WPF and migrated from WinForms. Still trying to do some intermediate WPF stuff.
Thanks.
You was close
<SolidColorBrush x:Key="FrmBdy" Color="{StaticResource DarkH}" />

WPF SolidColorBrush bind to another StaticResource

I have question about SolidColorBrush.
I have Resource:
<SolidColorBrush x:Key="Accent3" Color="#0093DD"/>
And I would like to bind another resource to this one like this:
<SolidColorBrush x:Key="AccentColorBrush" Color="{Binding Source={StaticResource Accent3}}" />
Both of them are in same file, AccentColorBrush is below Accent3.
How should I do that?
You can create a color and then bind it to both your colorbrush...something like this:
<Color x:Key="YourColor">#0093DD</Color>
<SolidColorBrush x:Key="Accent3" Color="{StaticResource YourColor}"/>
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource YourColor}" />

Hiding the selection in a WPF ListBox, keeping it in contained controls

I'm using a ListBox to display a list of editable objects whose template contains, among other things, a ComboBox.
I used this common technique to hide the ListBox selection, which is not used for anything:
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Resources>
<Brush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</Brush>
<Brush x:Key="{x:Static SystemColors.ControlBrushKey}">Transparent</Brush>
The problem is this messes with the ComboBox dropdown list selection.
I'd like to override these resources again in my template, specifying the original values (SystemColors.HighlightBrush, etc) instead of hardcoding them. How can I do that?
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type SearchService:Criterion}">
<DataTemplate.Resources>
<!--I know how to specify a hardcoded brush here,
but not how to reference one from SystemColors-->
I used this common technique to hide
the ListBox selection, which is not
used for anything
If you do not use the selection for anything you should just use an ItemsControl instead.
You could do:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="{x:Static SystemColors.HighlightColor}" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{x:Static SystemColors.ControlColor}" />
or
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}" />
To restore the brushes to their default colors.

I get following error: '#FF000000' is not a valid value for Color

I sometime get following error message:
Cannot convert the value in attribute
'Color' to object of type
'System.Windows.Media.Color'.
'#FF000000' is not a valid value for
property 'Color'. Error at object
'HighlightTextBrush' in markup file
The WPF code for HighlightTextBrush is:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="{StaticResource {x:Static SystemColors.ControlTextBrushKey}}" />
You are trying to assign a Brush to the Color property. You need to use:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="{StaticResource {x:Static SystemColors.ControlTextColorKey}}" />
I had a problem using CodeNaked's answer where it returned the same error. I used this instead:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding Source={x:Static SystemColors.ControlTextColorKey},Path=Color}" />

Make highlight brush the same as control brush without specifying colour

I want to ensure that a selected ListViewItem's non-focused background is the same as the focused background. I know that the common way of doing this is as follows:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue"/>
However, the problem is that I don't want to specify a colour, I merely want the Brush returned by the static resource whose key is ControlBrushKey to be the same as the one for HighlightBrushKey.
The answer is this:
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" />
Try this... I know it works to set two properties to match, not sure if it will work in your context, but it's worth a shot:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Blue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{DynamicResourse SystemColors.HighlightBrushKey.Color}"/>
I tested this using a TextBox as a playground. I'm not sure of your exact application, but here was my test markup:
<TextBox>
<TextBox.Background>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Blue"></SolidColorBrush>
</TextBox.Background>
<TextBox.Foreground>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{DynamicResource SystemColors.HighlightBrushKey.Color}" />
</TextBox.Foreground>
</TextBox>
This just set the background to blue, and the foreground to the background, which was the expected result.

Resources