Quantcast
Channel: User Liyun Zhang - MSFT - Stack Overflow
Viewing all articles
Browse latest Browse all 445

Answer by Liyun Zhang - MSFT for Binding ListView to ObservableCollection with MVVM

$
0
0

The x:DataType="vm:UserList_ViewModel" in the <ContentPage> will make the ContentPage and the children view bind to type of UserList_ViewModel.

You can declare the datatype for the ListView's item DataTemplate to make the item bind to type of User:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             x:Class="WaiterEval.Views.UserList"             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"             xmlns:vm="clr-namespace:WaiterEval.ViewModels"             xmlns:models="clr-namespace:WaiterEval.Models"              x:DataType="vm:UserList_ViewModel"><VerticalStackLayout><ListView x:Name="userList" ItemsSource="{Binding Users}"><ListView.ItemTemplate><DataTemplate x:DataType="models:User">                    ....</DataTemplate></ListView.ItemTemplate></ListView></VerticalStackLayout></ContentPage>

Viewing all articles
Browse latest Browse all 445

Trending Articles



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