Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Null-safe binding to a nested property

$
0
0

I have a binding like this:

var nameLabel = new Label();
nameLabel.SetBinding(
  Label.TextProperty,
  new Binding("Item.Location.PathElement.Name",
              BindingMode.Default,
              new Null2DefaultConverter(s_DefaultValueNoPath)));

The problem is that all levels of the sub-properties of Item can be null. In my case, the PathElement property returns null. Currently that causes the binding to fail completely, so that my converter is not called. Changing the path to "Item.Location?.PathElement?.Name" did not help either.

In WPF XAML there are 2 mechanisms in the Binding class that handle nulls and errors:
* TargetNullValue provides a default value if the value is successfully retrieved but was null (in my example Name is null)
* FallbackValue provides a default value if the value could not be retrieved (e. g. if any property "before" Name was null already)

How can I make the Xamarin binding assume null as value when any part of the nested property path is null?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>