2

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?

sarath
  • 498
  • 2
  • 12
  • 19

2 Answers2

4

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: What's the simplest way to display NULL values as "NULL" with WPF Data Binding?

Community
  • 1
  • 1
Val
  • 930
  • 6
  • 10
  • Hi val, Thanks for the easiest way!. BTW may I know why it wouldnt work in my way? – sarath Sep 14 '10 at 12:52
  • 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 Sep 14 '10 at 15:51
  • Yes, but you haven't explicitly bound the Background property in the columns declaration. So there's nothing to interfere with that working. – Val Sep 15 '10 at 00:27
  • oh! so is there a way I can fix it? Or go with your solution? – sarath Sep 15 '10 at 01:57
  • 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 Sep 15 '10 at 02:13
3

Namespace xmlns:sys="clr-namespace:System;assembly=mscorlib"

<DataGridTextColumn Header="Time taken" Binding="{Binding Path=finish}" Width="Auto"     x:Name="x">
 <DataGridTextColumn.ElementStyle>
      <Style TargetType="{x:Type TextBlock}">
          <Setter Property="Text" Value="{Binding Path=finish}"></Setter>
          <Style.Triggers>
              <DataTrigger Binding="{Binding Path=finish}" Value="{x:Static sys:String.Empty}">
                   <Setter Property="Text" Value="-"></Setter>
               </DataTrigger>
           </Style.Triggers>
        </Style>
 </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
Bindya
  • 135
  • 2
  • 14
  • May I know why you added this answer? – sarath Apr 17 '13 at 01:00
  • Since element style is overridden you need to set the text value and then set the trigger for checking for null or empty data for the datagrid cell. – Bindya Apr 25 '13 at 10:12