can someone please tell me the exact difference between FallbackValue
and TargetNullValue
? I know they are quite similar, but I'd like to understand more about those edge usecases where you want to use either one of the two or maybe usecases where using both makes sense.
Cheers!
CodePudding user response:
Basically,
FallbackValue
is set when the binding itself fails.TargetNullValue
is set when the source of the binding is null.
These are the results I got with WinUI 3.
Plain binding
public string TestText { get; set; } = "Binding succeeded!";
<TextBlock Text="{x:Bind TestText}" />
OR
<TextBlock Text="{Binding TestText}" />
The TextBlock
shows "Binding succeeded".
Case#1
x:Bind
, FallbackValue
/TargetNullValue
, wrong name
public string? TestText { get; set; } = "Binding succeeded!";
<TextBlock Text="{x:Bind Test, FallbackValue='Binding failed!'}" />
OR
<TextBlock Text="{x:Bind Test, TargetNullValue='Source is null!'}" />
You get a compile error because x:Bind
checks the source at compile time.
Case#2
x:Bind
, FallbackValue
, null source
public string? TestText { get; set; } = null;
<TextBlock Text="{x:Bind TestText, FallbackValue='Binding failed!'}" />
The TextBlock
shows nothing (empty).
Case#3
Binding
, FallbackValue
, wrong name
public string TestText { get; set; } = "Binding successed!";
<TextBlock Text="{Binding Test, FallbackValue='Binding failed!'}" />
The TextBlock
shows the FallbackValue
"Binding failed!".
Case#4
x:Bind
, TargetNullValue
, null source
public string? TestText { get; set; } = null;
<TextBlock Text="{x:Bind TestText, TargetNullValue='Source is null!'}" />
The TextBlock
shows the TargetNullValue
"Source is null!".
Case#5
Binding
, TargetNullValue
, null source
public string? TestText { get; set; } = null;
<TextBlock Text="{Binding TestText, TargetNullValue='Source is null!'}" />
The TextBlock
shows nothing (empty).
Case#6
Binding
, FallbackValue
, TargetNullValue
, null source
public string? TestText { get; set; } = null;
<TextBlock Text="{Binding TestText, FallbackValue='Binding failed!', TargetNullValue='Source is null!'}" />
The TextBlock
shows the FallbackValue
"Binding failed!".
Case#7
x:Bind
, FallbackValue
, TargetNullValue
, null source
public string? TestText { get; set; } = null;
<TextBlock Text="{x:Bind TestText, FallbackValue='Binding failed!', TargetNullValue='Source is null!'}" />
The TextBlock
shows the TargetNullValue
"Source is null!".
Case#8
x:Bind
/Binding
, TargetNullValue
, null ViewModel
public ViewModel? ViewModel { get; set; } = null;
<TextBlock Text="{x:Bind ViewModel.TestText, TargetNullValue='Source is null!'}" />
The TextBlock
shows nothing (empty).
Case#9
x:Bind
/Binding
, FallbackValue
, null ViewModel
public ViewModel? ViewModel { get; set; } = null;
<TextBlock Text="{x:Bind ViewModel.TestText, FallbackValue='Binding failed!'}" />
The TextBlock
shows the FallbackValue
"Binding failed!".