7

Could anyone explain to me why this does not render "VALUE IS DEFAULT"?

<TextBlock Text="{Binding Fail, StringFormat=VALUE IS {0}, FallbackValue=DEFAULT}" />

There is something tricky about this syntax I am missing. Thank you in advance.

3 Answers 3

7

Binding in WPF does not consider StringFormat while falling back to FallbackValue in case it fails.

You can use what leon suggested or go with PriorityBinding.

--EDIT--

This should work:

<TextBlock DataContext="{Binding Fail, FallbackValue=DEFAULT}" Text="{Binding StringFormat=VALUE IS {0}}" />
Sign up to request clarification or add additional context in comments.

Comments

1

I think it could also work using the runs inside the TextBlock :

     <TextBlock>
             <Run Text="Value is : "/>
             <Run Text="{Binding Fail,FallbackValue=Default}"/>
     </TextBlock>

?

Comments

0

The default fallback value is used for priority bindings, if you'd like to display "VALUE IS DEFAULT" for a fallback value, try the following.

<TextBlock Text="{Binding Fail, StringFormat=VALUE IS {0}, FallbackValue='VALUE IS DEFAULT'}" />

2 Comments

Do you mean if the FallbackBinding is used the StringFormat is not?
Yes, I have verified that StringFormat is ignored when FallbackValue

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.