WPF ComboBox: background color when disabled - wpf

I currently use this style for my ComboBox in WPF:
<Style TargetType="ComboBox">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#303030"/>
<Setter Property="BorderBrush" Value="#000000"/>
</Style>
How can I change it to specify the background color when the ComboBox is disabled?
(this is a follow-up to this question: WPF combobox colors)

<Style TargetType="ComboBox">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#303030"/>
<Setter Property="BorderBrush" Value="#000000"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="#101010"/>
</Trigger>
</Style.Triggers>
</Style>

I ended up using the style used here as a base, and this did allow to set the background color when disabled:
http://msdn.microsoft.com/en-us/library/ms752094%28v=VS.85%29.aspx

Related

Removing box / border around selected cell contents in WPF DataGrid

Trying to restyle a WPF datagrid. Almost where I need to be, except for one peculiarity: when selecting a row, the contents of all the cells are surrounded by a white border.
I'm at a loss as to how to get rid of this. At the moment, my styling looks like this:
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#3CACDC" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#90C8E0" />
</Trigger>
</Style.Triggers>
</Style>
Plus DataGridCell has an almost identical style set.
It's not clear whether the question How to suppress DataGrid cell selection border? is asking the same question, but the accepted solution of setting FocusVisualStyle to Null doesn't remove the border. It does, however, change the style:
How can I get ride of that border?
I think you can try styling DataGridCell style
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>
</DataGrid.CellStyle>

WPF TextBox Style When Disabled

I'm trying to create a simple TextBox style. I want to create a trigger to change the colors when the textbox is disabled:
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="{StaticResource NormalFontSize}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FontStyle" Value="Normal"/>
<Setter Property="FontFamily" Value="{StaticResource FontFamilyName}"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{StaticResource listItemHighlightBackground}"/>
<Setter Property="Foreground" Value="{StaticResource disabledArrowBackground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
When I use the trigger, the textbox disappears
What's wrong here?
Thanks
A ControlTemplate replaces the template for that control type. It looks like you're misunderstanding its usage. Before the triggers, try adding some visual objects to display, such as a border, and that will appear. Better yet, don't use a ControlTemplate at all...you only need to set a property trigger on the style, e.g:
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property = "Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>

WPF DataGrid RowStyle for selected row not changing the background and foreground color

I am using Visual Studio 2012 on windows 7. I need to know why the following style for Grid's selected row does not work for background and foreground colors but works perfectly fine for other properties like BorderBrush and BorderThickness etc? Though I can see them changing while mouse over grid rows.
<Style x:Key="gridRowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
<Setter Property="BorderBrush" Value="BlueViolet" />
<Setter Property="BorderThickness" Value="2" />
</Trigger>
</Style.Triggers>
</Style>
Here is how I am using on grid.
<DataGrid RowStyle="{StaticResource gridRowStyle}">
I am stressing on to know "why" rather than solution to the problem as I already have solution to problem if I use grid cell style instead of rowstyle like following:
<Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="PeachPuff"/>
<Setter Property="Foreground" Value="BlueViolet"/>
</Trigger>
</Style.Triggers>
</Style>
In the Default style of the DataGridCell having the following default style trigger.
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
So if you written trigger for DataGridRow, then it will only apply to the element that was placed before the DataGridCell in visual tree.
So to change the Background & Foreground while selection, you must write the trigger in DataGridCell style or remove the default trigger from the style.
Simply remove this attributs at row level in the datagrid, they have priority over the trigged properties.
nezac

Coloring of ListBox

Is it possible to get this row coloring in the WPF Listbox?
White
LightGray
Gray
White
LightGray
etc.?
Thanks
Yes it's possible. You can use the AlternationCount Property of the ListBox. Something like
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="White"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="Gray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
And then just set the AlternationCount on your ListBox
<ListBox AlternationCount="3"
...>

How do I get a white WPF ComboBox DropDown arrow color?

I am trying to work out a style for a ComboBox that has a navy background with white text, so I want the drop down arrow to be white also (the xaml I have so far is below).
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="Background" Value="{StaticResource headerBrush}"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderBrush" Value="{StaticResource headerBorderBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="MinWidth" Value="100"/>
<Setter Property="Height" Value="21"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
<Setter Property="Background" Value="AliceBlue"/>
<Setter Property="Foreground" Value="Navy"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
ADDED code to set the ControlTemplate?
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Path x:Name="Arrow" Fill="White"/>
</ControlTemplate>
</Setter.Value>
</Setter>
You need to edit the ControlTemplate of ComboBox and you can see a the Arrow as a Path. So change the Fill property of the Path to your desired arrow color. See sample ControlTemplate here
http://msdn.microsoft.com/en-us/library/ms752094.aspx

Resources