biju
biju

Reputation: 18010

How to hide stringformat if data is null

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

Upvotes: 19

Views: 13712

Answers (4)

dave
dave

Reputation: 221

TargetNullValue=''

Will do also

Upvotes: 22

biju
biju

Reputation: 18010

"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}}" />

Upvotes: 34

naacal
naacal

Reputation: 620

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

Upvotes: 1

rudigrobler
rudigrobler

Reputation: 17133

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

Upvotes: 35

Related Questions