XAML items controls; bind to a C++/WinRT collection

A collection that can be effectively bound to a XAML items control is known as an observable collection. This idea is based on the software design pattern known as the observer pattern. This topic shows how to implement observable collections in C++/WinRT, and how to bind XAML items controls to them (for background info, see Data binding).

If you want to follow along with this topic, then we recommend that you first create the project that's described in XAML controls; bind to a C++/WinRT property. This topic adds more code to that project, and it adds to the concepts explained in that topic.


For essential concepts and terms that support your understanding of how to consume and author runtime classes with C++/WinRT, see Consume APIs with C++/WinRT and Author APIs with C++/WinRT.

What does observable mean for a collection?

If a runtime class that represents a collection chooses to raise the IObservableVector<T>::VectorChanged event whenever an element is added to it or removed from it, then the runtime class is an observable collection. A XAML items control can bind to, and handle, these events by retrieving the updated collection and then updating itself to show the current elements.


For info about installing and using the C++/WinRT Visual Studio Extension (VSIX) and the NuGet package (which together provide project template and build support), see Visual Studio support for C++/WinRT.

Add a BookSkus collection to BookstoreViewModel

In XAML controls; bind to a C++/WinRT property, we added a property of type BookSku to our main view model. In this step, we'll use the winrt::single_threaded_observable_vector factory function template to help us implement an observable collection of BookSku on the same view model.

Declare a new property in BookstoreViewModel.idl.

// BookstoreViewModel.idl
runtimeclass BookstoreViewModel
    BookSku BookSku{ get; };
    Windows.Foundation.Collections.IObservableVector<BookSku> BookSkus{ get; };


In the MIDL 3.0 listing above, note that the type of the BookSkus property is IObservableVector of BookSku. In the next section of this topic, we'll be binding the items source of a ListBox to BookSkus. A list box is an items control, and to correctly set the ItemsControl.ItemsSource property, you need to set it to a value of type IObservableVector, or IVector, or of an interoperability type such as IBindableObservableVector.


The code shown in this topic applies to C++/WinRT version 2.0.190530.8 or later. If you're using an earlier version, then you'll need to make some minor tweaks to the code shown. In the MIDL 3.0 listing above, change the BookSkus property to IObservableVector of IInspectable. And then use IInspectable (instead of BookSku) in your implementation, too.

Save and build. Copy the accessor stubs from BookstoreViewModel.h and BookstoreViewModel.cpp in the \Bookstore\Bookstore\Generated Files\sources folder (for more details, see the previous topic, XAML controls; bind to a C++/WinRT property). Implement those accessor stubs like this.

// BookstoreViewModel.h
struct BookstoreViewModel : BookstoreViewModelT<BookstoreViewModel>

    Bookstore::BookSku BookSku();

    Windows::Foundation::Collections::IObservableVector<Bookstore::BookSku> BookSkus();

    Bookstore::BookSku m_bookSku{ nullptr };
    Windows::Foundation::Collections::IObservableVector<Bookstore::BookSku> m_bookSkus;
// BookstoreViewModel.cpp
    m_bookSku = winrt::make<Bookstore::implementation::BookSku>(L"Atticus");
    m_bookSkus = winrt::single_threaded_observable_vector<Bookstore::BookSku>();

Bookstore::BookSku BookstoreViewModel::BookSku()
    return m_bookSku;

Windows::Foundation::Collections::IObservableVector<Bookstore::BookSku> BookstoreViewModel::BookSkus()
    return m_bookSkus;

Bind a ListBox to the BookSkus property

Open MainPage.xaml, which contains the XAML markup for our main UI page. Add the following markup inside the same StackPanel as the Button.

<ListBox ItemsSource="{x:Bind MainViewModel.BookSkus}">
        <DataTemplate x:DataType="local:BookSku">
            <TextBlock Text="{x:Bind Title, Mode=OneWay}"/>

In MainPage.cpp, add a line of code to the Click event handler to append a book to the collection.

// MainPage.cpp
void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
    MainViewModel().BookSku().Title(L"To Kill a Mockingbird");
    MainViewModel().BookSkus().Append(winrt::make<Bookstore::implementation::BookSku>(L"Moby Dick"));

Now build and run the project. Click the button to execute the Click event handler. We saw that the implementation of Append raises an event to let the UI know that the collection has changed; and the ListBox re-queries the collection to update its own Items value. Just as before, the title of one of the books changes; and that title change is reflected both on the button and in the list box.

Important APIs