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

1
  • What are we considering? Commented Sep 15, 2010 at 18:23

4 Answers 4

35

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

Sign up to request clarification or add additional context in comments.

Comments

35

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

2 Comments

Seems like you should give credit to rudigrobler and update your original post with these details.
Don't forget to add this namespace: xmlns:System="clr-namespace:System;assembly=mscorlib"
22
TargetNullValue=''

Will do also

Comments

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.