Textblock DataTriggers not firing - c#

I'm trying to bind some data to a TextBlock. The TextBlock is to display a slider bar value, and when the slider bar value has changed I want the TextBlock text to change colour to red.
My XAML is like this:
<Grid Height="227">
<TextBlock Margin="114,60,112,150" Name="textBlock1" Text="{Binding Path=DispVal}" Width="42" Grid.Column="1" HorizontalAlignment="Center" TextAlignment="Center" FontWeight="Bold">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsChanged}" Value="true">
<Setter Property="TextBlock.Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsChanged}" Value="false">
<Setter Property="TextBlock.Foreground" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
I have bound the TextBlock using DataContext:
texBlock1.DataContext = m_slider;
I update the m_slider object when my slider bar update handler fires.
However, I don't get any text or colour changes.

you could replace textblock with textbox and make it similar in look and feel by applying the following properties
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
Then you can use the TextChanged event in order to update its style any time text is changed by your binding

Related

WPF isn't recognize static resource of "Red" as Red brush in trigger condition

I got this TextBlock:
<TextBlock Foreground="Red"/>
And there is an implicit style for TextBlock with style trigger that
asks "if the foreground is {StaticResource BrushTextBlockAlertForeground}
then set the background to black."
(BrushTextBlockAlertForeground is Red, of course).
<Trigger Property="Foreground" Value="{StaticResource BrushTextBlockAlertForeground}">
<Setter Property="Background" Value="Black"/>
</Trigger>
This trigger condition fails!
If static resource resolved on loading, so why this trigger fails?
Shouldn't the XAML loader put there Red in the trigger condition? or it puts some expression instead?
Is there any chance that happening because the "Value" property of trigger condition isn't dependency property?
Only when I write
<Trigger Property="Foreground" Value="Red">
<Setter Property="Background" Value="Black"/>
</Trigger>
It works.
If I put static resource from outside (look below), it doesn't work in any case. Like that:
<TextBlock Foreground="{StaticResource BrushTextBlockAlertForeground}"/>
I would love to know the reason behind, because I want to write reusable color, instead of putting "red" in many places. "Tomorrow" someone will try to make that reusable and will encounter the behavior I experiencing.
Ensure, TextBlocks you test and StyleTrigger use both(!) the same brush Red or from StaticResource. TextBlock with foreground Red and StyleTrigger with StaticResource and vice versa will not work, because the values Brushes.Red and from StaticResource aren't equal. See A: How to Compare SolidColorBrushes?
<StackPanel>
<!--this doesn't work-->
<StackPanel.Resources>
<SolidColorBrush x:Key="forebr" Color="Red"/>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Foreground" Value="{StaticResource forebr}">
<Setter Property="Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock Foreground="Red" Text=" Test trigger 0"></TextBlock>
</StackPanel>
<StackPanel>
<!--this doesn't work-->
<StackPanel.Resources>
<SolidColorBrush x:Key="forebr" Color="Red"/>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Foreground" Value="Red">
<Setter Property="Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock Foreground="{StaticResource forebr}" Text=" Test trigger 1"></TextBlock>
</StackPanel>
<StackPanel>
<!--this works-->
<StackPanel.Resources>
<SolidColorBrush x:Key="forebr" Color="Red"/>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Foreground" Value="{StaticResource forebr}">
<Setter Property="Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock Foreground="{StaticResource forebr}" Text=" Test trigger 2"></TextBlock>
</StackPanel>
<StackPanel>
<!--this works-->
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Foreground" Value="Red">
<Setter Property="Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock Foreground="Red" Text=" Test trigger 3"></TextBlock>
</StackPanel>

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.

Better Binding for DataTrigger

I have a TextBlock and I want to set the property Visibility to Collapsed when the TextBlock has no text. I wonder me, for sure there should be a better way to check if the Lenght of the property Text is equal than 0.
<TextBlock Name="TextBlockHeader" Foreground="White" FontSize="18" FontWeight="Bold" Text="{Binding Header}" Margin="0,0,0,25">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TextBlockHeader, Path=Text.Length}" Value="0">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Here I have to define a name for the TextBlock and I can reference it in the Datatrigger Binding="{Binding ElementName=TextBlockHeader, Path=Text.Length}"
But how can I achieve the same without having to define a name for the TextBlock?
You would usually use Triggers instead of DataTriggers, and compare the Text property to either null or an empty string.
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Style.Triggers>
</Style>
As the TextBlock class seems to coerce the Text property value to be non-null, it may be sufficient to have only the second Trigger for an empty string.

How to add style to datagridtemplatecolumn (WPF XAML)

How to add style to datagridtemplatecolumn (WPF,XAML)
I have a datagrid where I am using datagridtemplate column as inside it I need a textblock and a button.
The textblock gets its data from an another view which is working fine. But the textblock is unable to have any scroll/textwrapping as I am unable to add any style to the textblock.
As a result when this textblock is getting multiline data only one line is visible.
I am looking for a functionality similar to datagridtextcolumn.elementstyle.Please advise.
I use a DataGridTemplateColumn to place an image of a trash can on every row of the grid. You could put a Button with an image as its content, instead of an Image in this example. When the user clicks on the image it will delete the row from the observable collection.
You will need to add
xmlns:i="clr-amespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform" in order to use the interaction. But you can set the styles of the textblock, the cell and the image/button as in the Xaml below.
Hope this helps.
<DataGridTemplateColumn MinWidth="30" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGridCell Style="{StaticResource YOURCellStyle}" >
<TextBlock Style="{StaticResource YOURTextBlockStyle}">
<Image Width="12"
Margin="0,0,0,0"
HorizontalAlignment="Center"
Source="pack://application:,,,/Resources/Images/DialogIcons/rubbish-bin.PNG"
Stretch="Uniform"
Style="{StaticResource YOURImageStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding
Path=DataContext.YOURDeleteComand, ElementName=YOURDataGrid}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</TextBlock>
</DataGridCell>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<Style x:Key="YourImageOrButtonStyle" TargetType="ImageOrButton">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="ToolTip" Value="Delete"/>
</Style>
<Style x:Key="YOURCellStyle" TargetType="DataGridCell">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="YOURTextBlockStyle" TargetType="TextBlock">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>

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