17

How to hide a stringformat when data is not present.Consider this sample

<TextBlock Text="{Binding Amount, StringFormat=Total: {0:C}}" />

in this case if Amount is null,Then it will show just Total:.How to hide this if Amount is null or empty

biju
  • 17,554
  • 10
  • 59
  • 95

4 Answers4

35

You either have to implement some sort of value converter (Example) or consider using the TargetNullValue property on the binding (Example)

Community
  • 1
  • 1
rudigrobler
  • 17,045
  • 12
  • 60
  • 74
32

"TargetNullValue" is what i was looking for.I ended up with this and it worked like a charm

<TextBlock VerticalAlignment="Top"
             Text="{Binding Path=TotalMonths,
        TargetNullValue={x:Static System:String.Empty},
        StringFormat=Total: {0:C}}" />
biju
  • 17,554
  • 10
  • 59
  • 95
22
TargetNullValue=''

Will do also

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
dave
  • 221
  • 2
  • 2
1

There's not much to work with here, but you can achieve something like this with:

  • DataTrigger
  • ValueConverter
  • EventHandling in Code-Behind
  • Binding on a (dependency-)property in a ViewModel encapsulating your business classes
naacal
  • 620
  • 1
  • 6
  • 16
  • I solved it using a value converter..but still helps to know any alternative approach...BTW cant handle in my business class since i am using datatables – biju Oct 14 '10 at 13:01