XAML items controls; bind to a C++/WinRT collection
Alt
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.
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.
Nóta
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.
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.
Rabhadh
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.
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.
Crie uma interface do usuário com associação de dados. Sua interface do usuário é atualizada automaticamente com base nos dados mais recentes, enquanto os dados são atualizados em resposta a alterações na interface do usuário.
Um valor escalar ou de matriz precisa ser encapsulado em um objeto de classe de referência antes de ser passado para uma função que espera **IInspectable**. Esse processo de encapsulamento é conhecido como **conversão** do valor.
Uma propriedade que pode ser efetivamente vinculada a um controle de itens XAML é conhecida como uma propriedade *observável*. Este tópico mostra como implementar e consumir uma propriedade observável e como associar um controle XAML a ela.
Este tópico orienta você pelas etapas de criação de um controle personalizado simples usando C++/WinRT. Você pode usar as informações aqui como base para criar seus próprios controles de interface do usuário personalizáveis e ricos em recursos.