sarath
sarath

Reputation: 508

setting text on DataGridTextColumn

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?

Upvotes: 2

Views: 18514

Answers (2)

Bindya
Bindya

Reputation: 145

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>

Upvotes: 3

Val
Val

Reputation: 930

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?

Upvotes: 4

Related Questions