Unable to style WPF ComboBox on Mouse Hover - c#

Does anyone know how to style the background property of a WPF ComboBox when a mouse is hovering on top of it?
I cannot get rid of the blue-ish button like background off the ComboBox.

You can style it like anything else:
<Style TargetType="{x:Type ComboBox}" x:Key="HoverBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
usage:
<ComboBox Style="{StaticResource HoverBox}" ... />
And at the top of your UserControl/ Window you have to place the style:
<UserControl...>
<UserControl.Resources>
<Style TargetType="{x:Type ComboBox}" x:Key="HoverBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
[CONTENT HERE]
</UserControl>

It will not work.Its because of the default control template of ComboBox.You may need to override the default template for this behavior.Have a look at
MouseOver highlighting style returning to default after a second (Caused by Aero?)
http://social.expression.microsoft.com/Forums/en/blend/thread/b210978c-24e8-431b-916b-a40a752b990c
http://social.msdn.microsoft.com/Forums/en/wpf/thread/a18891e9-8879-4819-9679-247341782f60

Related

How do I change the DataGrid cell hover colour in WPF?

How would I go about changing the datagrid hover colour in wpf?
I want the hover colour to go from this:
https://imgur.com/a/ZVKb8zx
To my own custom colour.
You can define style for DataGridCell as following:
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>

Hover effect in style doesn't work (ToolBarPanel) - WPF c#

I have a toolbarpanel which I've made a custom style.
<Window.Resources>
<Style x:Key="toolbar_opciones" TargetType="{x:Type ToolBarPanel}">
<Setter Property="Background" Value="DeepSkyBlue"/> <!-- does not work -->
<Setter Property="Cursor" Value="Hand"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="CadetBlue"/>
</Trigger> <!-- does not work -->
</Style.Triggers>
</Style>
</Window.Resources>
<ToolBarPanel Style="{StaticResource toolbar_opciones}"/>
The cursor works but the background property no, why?
I tested your code, and here is the fix
<ToolBarPanel Style="{StaticResource toolbar_opciones}"/>
look at it closely, you were missing the double quotation which close the Style attribute. When I added it to the XAML, the Background has been applied.
Cheers.

C# WPF - Disabled ComboBox should look like label

I'm trying to change the style of my combobox in wpf when it's disabled. It should look like a plain text (label).
Here is my code:
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Black" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Background" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
but it doesnt seem to work.
any hints?
You can add ControlTemplate, so the combobox will shown like a
TextBox(without border, background, toggle button, ect.). but it act
as a combobx(having drop-down list). Drop-down will not shown if the
control is disbled(hence it will shown like a label)
<ComboBox.Template>
<ControlTemplate>
<TextBlock Text="{Binding SelectedItem.MyText,RelativeSource={RelativeSource Mode=TemplatedParent}}"></TextBlock>
</ControlTemplate>
</ComboBox.Template>
If the control has a tendency to look like another control on certain condition, this kind of requirement can be satisfied by ContentControl. You can switch to appropriate content based on a given condition.
<ContentControl IsEnabled="True" /> // or IsEnabled="False"
Then switch via Style..
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="true">
// combobox
</Trigger>
<Trigger Property="IsEnabled" Value="false">
// Label
</Trigger>
</Style.Triggers>
</Style>
Just place two controls, the ComboBox and the Label. Bind the Visibility property of each to your boolean indicating if the ComboBox should be enabled so that one is visible when enabled and the other when disabled.

DataGrid -- AlternatingRowBackground color interfering with "IsMouseOver" color

I have a DataGrid that uses AlternatingRowBackground to make itself easier to read. In that same grid I also have a background color change for rows based on a "IsMouseOver" Setter Property in my App.xaml file. The problem that I am having is that the rows that have the alternating color (they are not white), do not change to the "IsMouseOver" color when the mouse hovers over it. Basically the AlternatingRowBackground color is taking precedence over my RowStyle. How do I make it so that the colored rows change as well when the mouse hovers over them?
App.xaml:
<!-- DataGrid Row Style -->
<Style x:Key="RowStyleWithAlternation" TargetType="DataGridRow">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Background" Value="GhostWhite"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="ContextMenu" Value="{x:Null}"/>
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFD0D0E0"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Purple"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#F9F99F" />
</Trigger>
</Style.Triggers>
</Style>
User Control xaml:
<DataGrid ... AlternatingRowBackground="Gray" RowStyle="{StaticResource RowStyleWithAlternation}" ... />
It works if you change your UserControl.xaml as follows:
<DataGrid RowStyle="{StaticResource RowStyleWithAlternation}" AlternationCount="2" />
The background is set through the AlternationIndex Trigger on your row which takes no priority over the IsMouseOver.
I've found the answer on this post:
WPF Style Trigger for DataGridRow Background Color Trumped by AlternatingRowBackground Brush

setting the TabItem IsSelected background

I can't seem to control the background color of the selected tab. I can use the IsSelected trigger to control the value of the non-selected tabs however.
This code:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="#EE444444" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="false">
<Setter Property="Background" Value="Pink"/>
</Trigger>
</Style.Triggers>
</Style>
works, in setting the un-selected tabs background to pink. However, the selected tabs following some light gray color I can't get rid of.
I also tried this:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="#EE444444" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="false">
<Setter Property="Background" Value="Pink"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
but none of these have any affect on selected tab. The only thing I can think of is that some referenced assembly has a generic tab style?
This style is located in the same file as the tab control, in the Grid.Resources section.
the TabItem selection behaviour is defined at the Template level. If you want to change the color, define a whole new DataTemplate, and define triggers in that template to change color. Then define that template as the ItemTemplate of your TabControl.

Categories

Resources