Click here to Skip to main content
13,299,898 members (62,836 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 8 Sep 2009

WPF: DataTrigger on a TextBlock!

, 8 Sep 2009
Rate this:
Please Sign up or sign in to vote.
So how does one add a DataTrigger to a TextBlock and provide some default value?

How does one add a DataTrigger to a TextBlock and provide some default value? Well, it's actually quite simple, but I have noticed that quite a few people fall down on this one. Firstly, you cannot have set a DataTrigger on anything that is not a Style, ControlTemplate, or DataTemplate. Great, so I will set up a Style for my TextBlock, right? The answer is yes, but you must ensure that you do not override the Text property in your TextBlock, and set the default value in the Style instead like this :

<style x:Key="StatusBlock"


  BasedOn="{StaticResource {x:Type TextBlock}}">
    <setter Property="Text" Value="Default Text!"/>
<style .Triggers>

      Binding="{Binding Path=CurrentView.IsRefreshing}"

          <setter Property="TextBlock.Text"

                Value="Status: Refreshing! (please wait)..."/>

Your TextBlock should be defined with the text not set, like this:

<textblock Style="{StaticResource StatusBlock}"/>


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Steve Psaltis
Software Developer
United Kingdom United Kingdom
We cannot achieve perfection but we can strive for excellence. Excellence through creativity that pushes innovation, collaboration that facilitates productivity, knowledge that empowers people and persistence that eventually pays off !

Enjoy what you do and do what you enjoy !

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Clifford Nelson5-Jun-12 9:23
memberClifford Nelson5-Jun-12 9:23 
GeneralMy vote of 3 Pin
Mahmudul Haque Azad9-Mar-11 20:48
memberMahmudul Haque Azad9-Mar-11 20:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 8 Sep 2009
Article Copyright 2009 by Steve Psaltis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid