Avoiding binding errors when part of the path is null
Posted: 03 Nov 2021, 17:36
Simplified version of my XAML:
That booleanToVisibilityConverter is not actually the default one but an extended one that interprets null as false.
So basically if Owner is null I want the whole thing to be collapsed, otherwise we'll display its Name.
When Owner is not null, everything works as expected.
When Owner is null, the visibility works correctly, however I get binding errors for both the ToolTip and the TextBlock.Text:
Setting FallbackValue and TargetNullValue on the bindings to an empty string doesn't help squelch the errors.
When running the same example with WPF I don't get any binding errors (to be fair I don't get any even if I remove the Visibility binding, so basically, no binding errors at all...).
Short of representing a missing Owner with a dummy value (which feels more like a workaround), I don't know how to fix this. Shouldn't FallbackValue kick in here?
I'm on version 3.0.12.
Edit: It appears that FallbackValue does kick in here, at least in my test I can see the FallbackValue when removing the Visibility binding and setting FallbackValue to 'foo' on the TextBlock.Text binding. So perhaps it is just the error reporting that is a bit too eager?
Code: Select all
<Border x:Name="owner" Height="20" Width="20"
Visibility="{Binding Owner, Converter={StaticResource booleanToVisibilityConverter}}"
ToolTip="{Binding Owner.Name}">
<TextBlock Text="{Binding Owner.Name}" />
</Border>
So basically if Owner is null I want the whole thing to be collapsed, otherwise we'll display its Name.
When Owner is not null, everything works as expected.
When Owner is null, the visibility works correctly, however I get binding errors for both the ToolTip and the TextBlock.Text:
Code: Select all
[PropertyPath.cpp:263]: [Noesis Warning] Property 'MyViewModel.Owner' returned null
[Binding.cpp:94]: [Noesis Error] Binding failed: Path=Owner.Name, Source=MyViewModel(''), Target=TextBlock(''), TargetProperty=TextBlock.Text
[PropertyPath.cpp:263]: [Noesis Warning] Property 'MyViewModel.Owner' returned null
[Binding.cpp:94]: [Noesis Error] Binding failed: Path=Owner.Name, Source=MyViewModel(''), Target=Border('owner'), TargetProperty=ToolTipService.ToolTip
When running the same example with WPF I don't get any binding errors (to be fair I don't get any even if I remove the Visibility binding, so basically, no binding errors at all...).
Short of representing a missing Owner with a dummy value (which feels more like a workaround), I don't know how to fix this. Shouldn't FallbackValue kick in here?
I'm on version 3.0.12.
Edit: It appears that FallbackValue does kick in here, at least in my test I can see the FallbackValue when removing the Visibility binding and setting FallbackValue to 'foo' on the TextBlock.Text binding. So perhaps it is just the error reporting that is a bit too eager?