views:

55

answers:

1

I am using a DataTrigger to replace empty cells with '-' text. My code:

  <DataGridTextColumn Header="Time taken" Binding="{Binding Path=finish}" Width="Auto" x:Name="x">
     <DataGridTextColumn.ElementStyle>
          <Style TargetType="{x:Type TextBlock}">
              <Style.Triggers>
                  <DataTrigger Binding="{Binding Path=finish}" Value="{x:Null}">
                       <Setter Property="TextBlock.Text" Value="-" />
                   </DataTrigger>
               </Style.Triggers>
            </Style>
     </DataGridTextColumn.ElementStyle>
  </DataGridTextColumn>

But I couldn't find the text being set. I tried changing the background of the TextBlock and its working. Why cant I set the Text property?

+1  A: 

The Binding in the column might be overriding the Setter.

But you don't need a data trigger to do this. As there is a property in the binding that you can set for these kinds of scenarios.

TargetNullValue allows you to set a value in the case that the bindings path is null.

Binding="{Binding Path=finish, TargetNullValue=Whatever you want}"

Taken From: http://stackoverflow.com/questions/416088/whats-the-simplest-way-to-display-null-values-as-null-with-wpf-data-binding

Val
Hi val, Thanks for the easiest way!. BTW may I know why it wouldnt work in my way?
Sarath
Well, What i've found with alot of data trigger based setters, is that if there is a binding in the original object on that property, the setter will fail to take affect. Thats a likely reason. Another might be the property "TextBlock.Text". That should probably just be "Text". Also slightly off topic, but you might find people are more inclined to offer answers if you have a higher "accept rate".
Val
Val- The trigger seems to work, otherwise I wouldnt get the background property set. And yeh, I even tried setting the property to 'Text'. Didnt work :(
Sarath
Yes, but you haven't explicitly bound the Background property in the columns declaration. So there's nothing to interfere with that working.
Val
oh! so is there a way I can fix it? Or go with your solution?
Sarath
In this instance it would be difficult to fix i think. Seeing as you're overriding a component of the column that will have bindings behind the scenes. If my solution works, then use it. Its a valid method.
Val
Thanks Val! : )
Sarath