Selected text color in WPF - wpf

Is it possible to set the selected text color for control like TextBox, ComboBox simply or do I have to setup a style trigger to change the selected text background.
I have a set of styles applied to the controls, but one of the things that isn't styled is the selected text, it's defaulted to a pale blue. I've tried setting some of the properties of the SystemColors class but this had no affect.
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightColor}"
Color="Red" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}"
Color="Red" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Orange" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrush}"
Color="Orange" />

It sounds like you're looking for the TextBoxBase.SelectionBrush property

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}" />

ComboBox with rounded corner

I am new to wpf and I wanted to change the ComboBox style to this However all I could do was to change the highlight color.
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF374046" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF374046" />
</ComboBox.Resources>
If anyone could guide me to understanding how to create it I would be thankful.
Have a quick glance at http://www.wpfhelper.com/index.php/styles-in-wpf/combobox/15-combobox-style-in-wpf. As mentioned in the comments, you need to create your own custom style & template for the ComboBox, ComboBoxItem and ComboBox ToggleButton. Play around with the CornerRadius of the popup element, to start with.

TreeView shows blue for selected item

I have a treeview structure. When I try to click on the nodes there is a blue color that shows the node selected. How can I remove that.
I don't want a selection color to be displayed on the tree.
ItemContainerStyle method does not work for me say on Windows-8. There are 4 brushes that generally correspond to this and are used by the default Template for TreeViewItem
keys:
HighlightBrushKey - Background with focus.
HighlightTextBrushKey - Foreground with focus.
InactiveSelectionHighlightBrushKey - Background without focus.
InactiveSelectionHighlightTextBrushKey - Foreground without focus.
Just override them as you see fit, for your requirement something like this would do fine:
<TreeView>
<TreeView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="Black" />
</TreeView.Resources>
</TreeView>
Do pay attention to only overriding them within the scope you require. For example if you put all this into App.xaml you're going to see some weird side-effects as all control's using these Brushes would now end up using your overridden ones which may not be what you're after.

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.

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