Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTrigger for Textblock

Tags:

c#

wpf

xaml

I have a Textblock that I'm trying to change the value of the Text property if a property is True or False. The issue I'm having is that the flag could be changed on different events on the screen (onchange events from other combo boxes).

I'm not sure how to get this datatrigger to work as I don't think it know when the value has been changed.

<TextBlock Grid.Row="9" HorizontalAlignment="Right" Text="Some Old Value:" VerticalAlignment="Center">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
             <Style.Triggers>
                 <DataTrigger Binding="{Binding IsChecked}" Value="False" >
                     <Setter Property="Text" Value="Different Text:"/>
                 </DataTrigger>
             </Style.Triggers>
         </Style>
     </TextBlock.Style>                                
</TextBlock>

I see in some comboboxes there is the UpdateSourceTrigger=PropertyChanged, but I don't see a way to implement that in the TextBlock.

like image 550
webdad3 Avatar asked Dec 30 '13 20:12

webdad3


People also ask

How does DataTrigger work?

A DataTrigger allows you to set property values when the property value of the data object matches a specified Value. For example, if you are displaying a list of Employee objects, you may want the foreground color to be different based on each Employee's current attendance.

What is TextBlock?

Text block is the primary control for displaying read-only text in apps. You can use it to display single-line or multi-line text, inline hyperlinks, and text with formatting like bold, italic, or underlined.

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.

What are wpf triggers?

The WPF styling and templating model enables you to specify triggers within your Style. Essentially, triggers are objects that enable you to apply changes when certain conditions (such as when a certain property value becomes true , or when an event occurs) are satisfied.


1 Answers

First of all set default Text in style setter, otherwise no matter whether your triggers fires successfully or not, Text won't take value from Style setter because of Dependency property value precedence order. Local value has higher precedence than style setter values.

<TextBlock Grid.Row="9" HorizontalAlignment="Right" VerticalAlignment="Center">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
             <Setter Property="Text" Value="Some Old Value:"/>
             <Style.Triggers>
                 <DataTrigger Binding="{Binding IsChecked}" Value="False" >
                     <Setter Property="Text" Value="Different Text:"/>
                 </DataTrigger>
             </Style.Triggers>
         </Style>
     </TextBlock.Style>                                
</TextBlock>

Second, if IsChecked property resides in your ViewModel (implementing INotifyPropertyChanged) and TextBlock DataContext is rightly pointing to ViewModel instance, you don't have to worry about it.

Just make sure whenever property IsChecked changes in ViewModel a PropertyChanged event gets raised so that UI can listen to that and updates itself.

like image 87
Rohit Vats Avatar answered Oct 07 '22 02:10

Rohit Vats