Hover effect in style doesn't work (ToolBarPanel) - WPF c# - 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.

Related

WPF Setter not working on some controls

I am trying to use a setter in my application to change to font size of all Controls. My Style is looking like this:
<Style x:Key="baseStyle" TargetType="{x:Type Control}">
<Setter Property="FontSize" Value="12" />
<Setter Property="Background" Value="{x:Null}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Width, ConverterParameter=1000, Converter={StaticResource DoubleToBoolConverter}, ElementName=window, Mode=OneWay}" Value="True">
<Setter Property="FontSize" Value="24" />
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
All of my substyles are looking like this
<Style BasedOn="{StaticResource baseStyle}" TargetType="{x:Type Button}">
...
</Style>
My problem is that my styles isnot applied to all controls. Labels for example seam to ignore my FontSize Setter
Default Style:
Triggered Style:
Do you actually refer to Label elements or TextBlock elements? Because the latter type is not a Control and won't be affected by your Style. This should work though:
<Style BasedOn="{StaticResource baseStyle}" TargetType="{x:Type Label}" />
But please always remember to provide a reproducible sample of your issue when asking a question: https://stackoverflow.com/help/mcve

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.

Trigger doesn't work on ToggleButton.IsChecked property

Here is a simple XAML with trigger that should change ToggleButton content when it is checked. But for some reason it doesn't work. I have a silly feeling that I missed something extra small. Appreciate your help
<ToggleButton Content="<">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value=">" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
You must move Content="<" from ToggleButton to setter of Style.
Example:
<ToggleButton>
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Content" Value="<" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value=">" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
Because local value has higher precedence order over Style setters and triggers:
Property system coercion.
Active animations, or animations with a Hold behavior.
3. Local value.
TemplatedParent template properties.
Implicit style.
6. Style triggers.
Template triggers.
8. Style setters.
...
For more information, please see:
MSDN: Dependency Property Value Precedence
You are overriding the Content set by the Trigger by setting the Content attribute at the control level. You want to set it using a Setter within the Style instead:
<ToggleButton>
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Content" Value="<" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value=">" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>

Disable the "IsEnabled=false" Color Change on a Button?

I want to Avoid the Color Change of a Button when it gets disabled.
The Button Color should be the Same if its diabled or not.
Using a style I can change the Background Color when it gets disabled:
<Style TargetType="{x:Type Button}">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
...
<ControlTemplate.Triggers>
...
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
<Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I can change the Background color there, but I want to keep it dynamic because the Background Color is databound and should not change.
If I delete the Background setter, the default background color change is performed.
How can I disable the colorchange? Or at least make the Disabled-Background-Color databound?
Sorry for my bad english.
If you want to make it data-bonund most appropriate place, imo, is declaring it inside relative DataTmplate of your control, where you specify the Style (already defined by you) and data applied to the control.
There you can define a Converter between some your state and relative color you would like to appear on the button.
OR
If you would like to limit yourself in Style, you can define a databinding inside Style itself. Imo not very logical, but it is possible.
Have a look on this answer:
Change Button Background color through MVVM pattern in WPF
I know this is late but hopefully it helps someone. This is how I would setup the style:
<Style TargetType="Button">
...
<Style.Triggers>
<Trigger TargetType="Button" Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
<Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</Style.Triggers>
</Style>
You need to include the TargetType again in the Trigger or else you'll get an exception.

Unable to style WPF ComboBox on Mouse Hover

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

Categories

Resources