C# WPF - Disabled ComboBox should look like label - c#

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.

Related

Set cursor when mouse over hyperlink in WPF Richt-TextBox

I have a Rich-Text-Box from Xtended WPF Toolkit to display text bound to some .rtf files. I would like to have the box "ReadOnly" but at the same time I want hyperlinks that are in the files to be active and clickable for the user. In order to achieve this I have the box created like this:
<xctk:RichTextBox x:Name="richTextBox" Cursor="Arrow" VerticalAlignment="Stretch" Text="{Binding text}" Focusable="False" IsDocumentEnabled="True">
<xctk:RichTextBox.Resources>
<Style TargetType="Hyperlink">
<Setter Property="Cursor" Value="Arrow" />
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="Hyperlink_MouseLeftButtonDown"/>
</Style>
</xctk:RichTextBox.Resources>
</xctk:RichTextBox>
Almost everything works fine and I can click on the links in the Rich-Text-Box, however when the mouse is over the link the cursor turns into a "Text Selection" cursor (the one just like when you hover over a text input field here) and that looks stupid. So it seems this line
<Setter Property="Cursor" Value="Arrow" />
is being ignored. Is there any way to fix this?
You should try to use this property
ForceCursor="true"
on your RichTextBox
Try to add an IsMouseOver trigger to the Style that sets the IsEnabled property to false:
<Style TargetType="Hyperlink">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="Hyperlink_MouseLeftButtonDown"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEnabled" Value="False" />
<Setter Property="Foreground" 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.

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.

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.

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