How to hide stringformat if data is null
Asked Answered
T

4

19

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

Terrazas answered 15/9, 2010 at 18:14 Comment(1)
What are we considering?Jot
T
34

"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}}" />
Terrazas answered 8/12, 2010 at 13:12 Comment(2)
Seems like you should give credit to rudigrobler and update your original post with these details.Imbecile
Don't forget to add this namespace: xmlns:System="clr-namespace:System;assembly=mscorlib"Honeysucker
B
35

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

Bracing answered 15/9, 2010 at 19:46 Comment(0)
T
34

"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}}" />
Terrazas answered 8/12, 2010 at 13:12 Comment(2)
Seems like you should give credit to rudigrobler and update your original post with these details.Imbecile
Don't forget to add this namespace: xmlns:System="clr-namespace:System;assembly=mscorlib"Honeysucker
G
22
TargetNullValue=''

Will do also

Gershon answered 13/12, 2010 at 10:9 Comment(0)
D
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
Dolley answered 15/9, 2010 at 19:50 Comment(1)
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 datatablesTerrazas

© 2022 - 2024 — McMap. All rights reserved.