Binding WPF to StringFormat

Hey guys, can someone explain to me why this does not "VALUE IS DEFAULT"?

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

There's something complicated about this syntax that I'm missing. Thank you in advance.

+6
source share
3 answers

Binding in WPF does not consider StringFormat, returning to FallbackValue if it fails.

You can use the leon clause or go with PriorityBinding.

- EDIT -

This should work:

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

I think it can also work using runs inside a TextBlock:

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

?

+1
source

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

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

Source: https://habr.com/ru/post/887214/


All Articles