What is the typical pattern for displaying an “unavailable” value when databinding in WPF?
For example I am using MVVM, and TextBlocks in my view are bound to my viewmodel’s properties, some of which are ints. There are times when I want to temporarily display two dashes (“–“) in my view rather than the property value.
I could change the property to be a string, and then in the getter add some logic to specify whether it returns the value or “–“. This is probably the appropriate way to use MVVM, but are there any easier ways?
Is there a way to take advantage of a TextBlock‘s FallbackValue? Or is there another approach for temporarily suspending databinding and displaying a “unavailable” value?
FallbackValueis only used when the binding path cannot be resolved, or the converter returnsDependencyProperty.UnsetValue. Converters are generally shunned when doing MVVM. My suggestion would be to have two properties, one that contains theintvalue and one that contains the display value:This gives you the best of both worlds. Your VM logic works with the
intproperty, but yourTextBlockbinds directly to astringrepresentation of the underlyingint.