.NET MAUI Databinding

I am able to bind the Realm Object directly from the QueryProperty

[QueryProperty(nameof(Owner), nameof(Owner))]

<Entry Text="{Binding Owner.Name, Mode=TwoWay}" FontSize=“Medium” />

I haven’t been able to get this to work for a field with a To-Many relationship.

Example: Owner.Authorizations

How do you recommend implementing this?

That sounds more like a MAUI question than a Realm one. You probably need some sort of IValueConverter that converts between your collection and a string. I’m not sure how you want your entry to behave - do you plan to concatenate the authorizations and then split the string by some character to recreate the collection when the user edits the text?

I was trying to-do a simple Entry with something like Binding {Owner.Authorizations.FirstOrDefault().Role

Ah, I see - I don’t believe this is possible via databinding. Your options are either to use a converter which looks something like:

public class AuthorizationsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is IEnumerable<Authorization> authorizations)
        {
            return authorizations.FirstOrDefault()?.Role;
        }

        throw new NotSupportedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then you’ll databind to Text={Binding Owner.Authorizations, Converter={StaticResource AuthorizationConverter}}. If you want this to be a two-way databinding, you’ll need to implement ConvertBack. Alternatively, you can add a FirstAuthorization computed property on your Owner model:

public partial class Owner : IRealmObject
{
    public IList<Authorization> Authorizations { get; }

    public Authorization? FirstAuthorization => Authorizations.FirstOrDefault();
}

And then databind to {Binding Owner.FirstAuthorization.Role}. One thing to be aware of with the second approach is that you wouldn’t get binding updates if the first authorization changes. If you need that, you’ll need to subscribe for change notifications on Authorizations and raise property changed for FirstAuthorization:

public Owner()
{
    (Authorizations as INotifyCollectionChanged).CollectionChanged += (s, e) =>
    {
        RaisePropertyChanged(nameof(FirstAuthorization));
    }
}

Disclaimer: I’m not very proficient with MAUI and there may be other ways to do it. I also didn’t test the code, so there may be typos.

Thanks for pointing me the right direction. What’s the chance you have Xamain.Forms sample application updated?