Docs Menu

Docs HomeRealm

Data Binding - .NET SDK

On this page

  • Data Binding Examples
  • Binding to a Single Realm Object
  • Binding to a Realm Collection

In Xamarin and MAUI applications, data binding enables UI updates when an underlying object or collection changes, and vice versa.

Realm objects and collections are live and update automatically to reflect data changes. This is because Realm objects implement INotifyPropertyChanged and Realm collections implement INotifyCollectionChanged. When you bind live collections and objects to your UI, both the UI and live objects and collections update at the same time.

Note

Compiled Bindings

The Realm SDK does not support Compiled Bindings.

The following code snippets show both the C# and XAML for three typical data-binding use cases. They all use the following Realm objects:

public partial class Employee : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("employee_id")]
[Required]
public string EmployeeId { get; set; }
[MapTo("name")]
[Required]
public string Name { get; set; }
[MapTo("items")]
public IList<Item> Items { get; }
}
public partial class Item : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("owner_id")]
[Required]
public string OwnerId { get; set; }
[MapTo("summary")]
[Required]
public string Summary { get; set; }
[MapTo("isComplete")]
public bool IsComplete { get; set; }
}

Note

Avoid Using ToList()

Because Realm collections are live, you should not call ToList() when obtaining a collection of Realm objects. Doing so forces loading the collection from memory, so the resulting collection is no longer a live object.

In the following example, we create a public property of type Employee. Elsewhere in our class, we query our realm for the Employee with an "EmployeeId" of "123":

public Employee Employee123 { get; }
...
Employee123 = realm.All<Employee>()
.FirstOrDefault(e => e.EmployeeId == "123");

In our XAML, we bind the Employee's Name property to a Label control's Text property:

<Label Text="{Binding Employee123.Name}"/>

There are two types of collections we can bind to. First, we can bind to a collection of Realm objects. In this code example, we create a public IEnumerable of the Employee Realm object and then populate that collection with all of the objects in the Employees collection:

public IEnumerable<Employee> Employees { get; }
...
Employees = realm.All<Employee>();

In the associated XAML file, we bind a ListView to the Employees IEnumerable to show a list of all Employees. In this case, we're listing each employee's "EmployeeId" value:

<ListView x:Name="listAllEmployees" ItemsSource="{Binding Employees}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding EmployeeId}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

We can also bind to a collection property of a Realm object. In this example, we bind to the Items property of a specific Employee. The Items property on the Employee class is of type IList<Item>.

public Employee Employee123 { get; }
...
IncompleteItems = Employee123.Items
.AsRealmQueryable()
.Where(i => i.IsComplete == false);

Note

Note that we are calling the AsRealmQueryable() method. This is required because we are filtering the collection. If we want to list all of the Items in the Items collection, we do not need to call AsRealmQueryable().

The XAML implementation is similar to the example above:

<ListView x:Name="listIncompleteItems" ItemsSource="{Binding IncompleteItems}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Summary}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
←  Change an Object Model - .NET SDKCRUD - .NET SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.