WPF : DataTrigger on a TextBlock!

VN:F [1.9.10_1130]
Rating: 0.0/10 (0 votes cast)

So how does one add a datatrigger to a TextBlock and provide some default value? Well its 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 a anything that is not a Style, ControlTemplate or DataTemplate. Great so i will set up a style for my TextBlock yes? 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><style .Triggers>
        <datatrigger 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}"/>
VN:F [1.9.10_1130]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.10_1130]
Rating: 0 (from 0 votes)

No Comment.

Add Your Comment