ListBox SelectedItem Background color scheme? - wpf

When we select ListBox item we get a Bluish background color right? How can i access that style and apply it to a different control?
i.e how can i use that selected item background color as a style elsewhere?
Thanks!

The ListBoxItem uses colors from the SystemColors class, which are pulled from Windows. Specifically, this trigger is what changes the background:
<Trigger Property="IsSelected"
Value="true">
<Setter TargetName="Bd"
Property="Background"
Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
So you can either use it like above (i.e. Background="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}", or you could access the colors directly (i.e. "{x:Static SystemColors.HighlightBrush}").
Same goes for code-behind.

I don't think this is a direct property so I believe what you will have to do is to change the whole template.
If you speak french, look at this link. I believe it contains everything you need to know :
http://www.developpez.net/forums/d899479/dotnet/developpement-windows/windows-presentation-foundation/wpf-listviewitem-definition-style/

Related

xctk:IntegerUpDown custom style to hide ButtonSpinner

On the xctk:IntegerUpDown, I would like the textbox border and the ButtonSpinner to only be visible when focused or mouseover.
It is easy enough to turn the border on/off using a <Style.Triggers> section.
It is also possible to control the ShowButtonSpinner property.
However, the content of the TextBox jumps to the right if I set ShowButtonSpinner=False.
I would like to simply hide the ButtonSpinner without TextBox contents jumping around.
Like this:
How can I get access to the appropriate property?
your question helped me to find ShowButtonSpinner property which I needed to hide up and down buttons
i can suggest a workaround with setting a fixed Padding for content when buttons are hidden. Value 0,0,17,0 seems ok to me (Win7, wpf toolkit version v2.6.0.0)
<xctk:IntegerUpDown.Style>
<Style TargetType="xctk:IntegerUpDown">
<Setter Property="Padding" Value="0,0,17,0"/>
<Setter Property="ShowButtonSpinner" Value="False"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Padding" Value="0"/>
<Setter Property="ShowButtonSpinner" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</xctk:IntegerUpDown.Style>
another simple thing is to align text to left side via property
<xctk:IntegerUpDown TextAlignment="Left"/>

wpf comboBox coloring issue

Due to requirements, I need to have a comboBox that works as follows:
It uses:
- one set of colors for fore/background when in view mode
- A second set of colors for fore/background when in edit mode
- Another set for selected mode (when the cursor is in the comboBox)
- Another set for disabled mode
The user will never be able to edit the contents, just click on the down arrow and select from the list.
I have the comboBox working except for the colors. Unlike other controls, simply trying to do the following (the triggers for edit mode) just doesn't work:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused"
Value="false" />
<Condition Property="wpfMisc:myCtrl.viewMode"
Value="false" />
<Condition Property="IsEnabled"
Value="true" />
</MultiTrigger.Conditions>
<Setter Property="BorderBrush"
Value="{DynamicResource controls-editableBorderBrush}" />
<Setter Property="Background"
Value="{DynamicResource controls-editableBackgroundBrush}" />
<Setter Property="Foreground"
Value="{DynamicResource controls-editableForegroundBrush}" />
</MultiTrigger>
What do I need to set in my style so that I can change the fore/back color of the displayed SelectedItem - i.e. make the above work?
And I am curious if anyone can tell me why a control like this doesn't use a similar interface as other data entry controls (isn't that the whole idea of polymorphism?) This isn't a big deal, just curious, that's all.
Thanks!
You don't achieve what you're after in the way that you are currently trying to achieve it. There is no need to use a MultiTrigger, just a number of sequential Trigger objects:
<ComboBox Width="150" Height="24">
<ComboBox.Style>
<Style>
<Setter Property="ComboBox.Background" Value="Green" />
<Style.Triggers>
<Trigger Property="ComboBox.IsFocused" Value="True">
<Setter Property="ComboBox.Background" Value="Red" />
</Trigger>
<Trigger Property="ComboBox.IsEnabled" Value="False">
<Setter Property="ComboBox.Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Now, I've shown you the IsEnabled Trigger here to demonstrate that you could add multiple Trigger objects like this. However, you can't actually use this Trigger for IsEnabled, because there is a Trigger defined inside the default ComboBox that already has a Trigger set on that property (to make it look disabled). If you absolutely have to add a Trigger for IsEnabled, then you will have to implement your own ControlTemplate for the ComboBox to override that default behaviour. If this is the case, please take a look at the ControlTemplate Class page on MSDN or ask a new question for help with this.
To address your other requirement of your 'view mode' is a bit more tricky. The code that you provided lookslike you are trying to retrieve the value directly from a class, rather than an instance of that class. In WPF, we normally add public properties into a view model or code behind file that we can bind to.
So I would imagine that you could have a bool property named IsViewMode and then you would add another Trigger like this:
<Trigger Property="IsViewMode" Value="True">
<Setter Property="ComboBox.Background" Value="Orange" />
</Trigger>
However, if your original syntax was correct, then your Trigger would look like this:
<Trigger Property="wpfMisc:myCtrl.viewMode" Value="True">
<Setter Property="ComboBox.Background" Value="Orange" />
</Trigger>

How would I increment a color value on MouseOver trigger in XAML template?

Hi all,
I am working on creating a button template to use in my WPF application. The format is basically color tiles (Think Windows Phone style for the most part.).
If I was wanting to change the color of the button in the event of a MouseOver, I would just make some code like this (let's say the original button color is Gray):
<Window.Resources>
<Style x:Key="ColorTileButton" TargetType="Button>
<!-- Insert various property editing here. -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False>
<Setter Property="Background" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</Style>
</Window.Resources>
But obviously this wouldn't work if the original button color was, say, Firebrick because I would end up with a bunch of standard Gray buttons. So I was wondering if there would be a way to rewrite this button template so that it would just increment the values of R, G, and B by about 10 to give the button a lighter color, then decrement is when the mouse left. Help me out?
Thanks guys.
I believe what you are describing is ColorAnimation an example, or you can look at this Forum Post. An interesting note from the Forum post.
A trigger makes a change to one or more properties when its condition is satisfied, and then resets the property values when its condition is no longer satisfied.
So what he is suggesting is:
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
it would reset to FireBrick as you gave in your example

UserControl's Item's background

I have a user control with few elements inside, one of the element I want to set it's background to Transparent, but I dont want the user of this control to be able to change it using Styles.
: for example
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="{StaticResource DisabledBackground}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForeground}" />
</Trigger>
this code sets the background of the entire control (including the item I dont want to change).
Any Ideas ?
Can't you just set the background of the control explicitly to transparent? That should override any inherited styles.

Make active control background colour change

I have a requirement to change the background colour of the active control (to make it easier to identify where the cursor is).
I've tried using a style with a trigger on the IsFocused property but I'm not having any luck at all; it doesn't seem to fire.
A XAML solution is most preferred.
I kept playing and this seems to work well :)
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="TextBox.Background" Value="Gray" />
</Trigger>
</Style.Triggers>
</Style>

Resources