英文:
Simplest way to bind a ListViews ItemSource property to an ObservableCollection in the CodeBehind Class
问题
我正在尝试找到在.NET MAUI中将ListView简单数据绑定到ObservableCollection的最简单和最优雅的方法。也许我有点被Web开发(Angular)宠坏了,那里数据绑定非常简单和高效。
我找到的最佳解决方案如下:
- 仅使用XAML方法。
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="..."
x:Name="page"
xmlns:services="..."
Title="...">
<ListView BindingContext="{x:Reference page}" ItemsSource="{Binding observableCollection}">
</ListView>
</ContentPage>
我真的很喜欢这种方法,因为我不必在代码后台文件中与Markup元素互动。这是我在另一种方法中找到的做法。
XAML:
<ListView x:Name="listView" ItemsSource="{Binding observableCollection}"></ListView>
代码后台文件:
public partial class ContentPage
{
// 编辑:这里不需要设置器
public ObservableCollection<type> observableCollection { get; }
public ContentPage()
{
InitializeComponent();
listView.BindingContext = this;
...
}
}
我想知道是否有更简单或更干净的解决方案?就像我在Web开发中习惯做的那样,可以直接将ListView的ItemSource属性绑定到代码后台类的ObservableCollection属性吗?
英文:
I'm trying to find the simplest and most elegant way to do a simple data-binding of a ListView to an ObservableCollection in .NET MAUI. Maybe I'm a little spoiled by web-development (Angular), where data-binding is just so easy and performant.
The best solutions I have found are the following:
- XAML only approach.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="..."
x:Name="page"
xmlns:services="..."
Title="...">
<ListView BindingContext="{x:Reference page}" ItemsSource="{Binding observableCollection}">
</ListView>
</ContentPage>
I really like this approach, because I don't have to interact with the Markup Elements in the code-behind file. This is what I'd do in the other approach I have found.
XAML:
<ListView x:Name="listView" ItemsSource="{Binding observableCollection}"></ListView>
Code-Behind File:
public partial class ContentPage
{
// Edit: no need for a setter here
public ObservableCollection<type> observableCollection { get; }
public ContentPage()
{
InitializeComponent();
listView.BindingContext = this;
...
}
}
I'm wondering if there is a simpler or cleaner solution? Like where I can directly bind the ListViews ItemSource property to the ObservableCollection property of the code-behind class (like I'm used to do in web-development)?
答案1
得分: 1
我们通常建议使用Model-View-ViewModel (MVVM)。
您可以参考以下代码:
- 创建一个ViewModel类(例如
MyViewModel.cs
)
public class MyViewModel {
public ObservableCollection<Item> Items { get; set; }
public MyViewModel() {
Items = new ObservableCollection<Item>();
Items.Add(new Item { NumType = "S", LocationCode = "0001" });
Items.Add new Item { NumType = "M", LocationCode = "0002" });
Items.Add(new Item { NumType = "L", LocationCode = "0003" });
Items.Add(new Item { NumType = "S", LocationCode = "0001" });
Items.Add(new Item { NumType = "M", LocationCode = "0002" });
Items.Add(new Item { NumType = "L", LocationCode = "0003" });
}
}
public class Item {
public string NumType { get; set; }
public string LocationCode { get; set; }
}
- 为当前页面(
MainPage.xaml
)设置BindingContext
,并为ListView设置ItemsSource
。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiListViewApp"
x:Class="MauiListViewApp.MainPage">
<ContentPage.BindingContext>
<local:MyViewModel></local:MyViewModel>
</ContentPage.BindingContext>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<ListView ItemsSource="{Binding Items}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding NumType}"
Detail="{Binding LocationCode}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</VerticalStackLayout>
</ContentPage>
英文:
We generally recommend using Model-View-ViewModel (MVVM).
You can refer to the following code:
-
create a ViewModelc class (e.g.
MyViewModel.cs
)public class MyViewModel { public ObservableCollection<Item> Items { get; set; } public MyViewModel() { Items = new ObservableCollection<Item>(); Items.Add(new Item { NumType = "S", LocationCode = "0001" }); Items.Add(new Item { NumType = "M", LocationCode = "0002" }); Items.Add(new Item { NumType = "L", LocationCode = "0003" }); Items.Add(new Item { NumType = "S", LocationCode = "0001" }); Items.Add(new Item { NumType = "M", LocationCode = "0002" }); Items.Add(new Item { NumType = "L", LocationCode = "0003" }); } } public class Item { public string NumType { get; set; } public string LocationCode { get; set; } }
-
set the
BindingContext
for current page(MainPage.xaml
) and setItemsSource
for the ListView.<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MauiListViewApp" x:Class="MauiListViewApp.MainPage"> <ContentPage.BindingContext> <local:MyViewModel></local:MyViewModel> </ContentPage.BindingContext> <VerticalStackLayout Spacing="25" Padding="30,0" VerticalOptions="Center"> <ListView ItemsSource="{Binding Items}" > <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding NumType}" Detail="{Binding LocationCode}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> </VerticalStackLayout> </ContentPage>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论