Simplest way to bind a ListViews ItemSource property to an ObservableCollection in the CodeBehind Class

huangapple go评论109阅读模式
英文:

Simplest way to bind a ListViews ItemSource property to an ObservableCollection in the CodeBehind Class

问题

我正在尝试找到在.NET MAUI中将ListView简单数据绑定到ObservableCollection的最简单和最优雅的方法。也许我有点被Web开发(Angular)宠坏了,那里数据绑定非常简单和高效。

我找到的最佳解决方案如下:

  1. 仅使用XAML方法。
  1. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3. x:Class="..."
  4. x:Name="page"
  5. xmlns:services="..."
  6. Title="...">
  7. <ListView BindingContext="{x:Reference page}" ItemsSource="{Binding observableCollection}">
  8. </ListView>
  9. </ContentPage>

我真的很喜欢这种方法,因为我不必在代码后台文件中与Markup元素互动。这是我在另一种方法中找到的做法。

XAML:

  1. <ListView x:Name="listView" ItemsSource="{Binding observableCollection}"></ListView>

代码后台文件:

  1. public partial class ContentPage
  2. {
  3. // 编辑:这里不需要设置器
  4. public ObservableCollection<type> observableCollection { get; }
  5. public ContentPage()
  6. {
  7. InitializeComponent();
  8. listView.BindingContext = this;
  9. ...
  10. }
  11. }

我想知道是否有更简单或更干净的解决方案?就像我在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:

  1. XAML only approach.
  1. &lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
  2. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
  3. x:Class=&quot;...&quot;
  4. x:Name=&quot;page&quot;
  5. xmlns:services=&quot;...&quot;
  6. Title=&quot;...&quot;&gt;
  7. &lt;ListView BindingContext=&quot;{x:Reference page}&quot; ItemsSource=&quot;{Binding observableCollection}&quot;&gt;
  8. &lt;/ListView&gt;
  9. &lt;/ContentPage&gt;

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:

  1. &lt;ListView x:Name=&quot;listView&quot; ItemsSource=&quot;{Binding observableCollection}&quot;&gt;&lt;/ListView&gt;

Code-Behind File:

  1. public partial class ContentPage
  2. {
  3. // Edit: no need for a setter here
  4. public ObservableCollection&lt;type&gt; observableCollection { get; }
  5. public ContentPage()
  6. {
  7. InitializeComponent();
  8. listView.BindingContext = this;
  9. ...
  10. }
  11. }

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)

您可以参考以下代码:

  1. 创建一个ViewModel类(例如 MyViewModel.cs
  1. public class MyViewModel {
  2. public ObservableCollection<Item> Items { get; set; }
  3. public MyViewModel() {
  4. Items = new ObservableCollection<Item>();
  5. Items.Add(new Item { NumType = "S", LocationCode = "0001" });
  6. Items.Add new Item { NumType = "M", LocationCode = "0002" });
  7. Items.Add(new Item { NumType = "L", LocationCode = "0003" });
  8. Items.Add(new Item { NumType = "S", LocationCode = "0001" });
  9. Items.Add(new Item { NumType = "M", LocationCode = "0002" });
  10. Items.Add(new Item { NumType = "L", LocationCode = "0003" });
  11. }
  12. }
  13. public class Item {
  14. public string NumType { get; set; }
  15. public string LocationCode { get; set; }
  16. }
  1. 为当前页面(MainPage.xaml)设置BindingContext,并为ListView设置ItemsSource
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:MauiListViewApp"
  5. x:Class="MauiListViewApp.MainPage">
  6. <ContentPage.BindingContext>
  7. <local:MyViewModel></local:MyViewModel>
  8. </ContentPage.BindingContext>
  9. <VerticalStackLayout
  10. Spacing="25"
  11. Padding="30,0"
  12. VerticalOptions="Center">
  13. <ListView ItemsSource="{Binding Items}" >
  14. <ListView.ItemTemplate>
  15. <DataTemplate>
  16. <TextCell Text="{Binding NumType}"
  17. Detail="{Binding LocationCode}" />
  18. </DataTemplate>
  19. </ListView.ItemTemplate>
  20. </ListView>
  21. </VerticalStackLayout>
  22. </ContentPage>
英文:

We generally recommend using Model-View-ViewModel (MVVM).

You can refer to the following code:

  1. create a ViewModelc class (e.g. MyViewModel.cs)

    1. public class MyViewModel {
    2. public ObservableCollection&lt;Item&gt; Items { get; set; }
    3. public MyViewModel() {
    4. Items = new ObservableCollection&lt;Item&gt;();
    5. Items.Add(new Item { NumType = &quot;S&quot;, LocationCode = &quot;0001&quot; });
    6. Items.Add(new Item { NumType = &quot;M&quot;, LocationCode = &quot;0002&quot; });
    7. Items.Add(new Item { NumType = &quot;L&quot;, LocationCode = &quot;0003&quot; });
    8. Items.Add(new Item { NumType = &quot;S&quot;, LocationCode = &quot;0001&quot; });
    9. Items.Add(new Item { NumType = &quot;M&quot;, LocationCode = &quot;0002&quot; });
    10. Items.Add(new Item { NumType = &quot;L&quot;, LocationCode = &quot;0003&quot; });
    11. }
    12. }
    13. public class Item {
    14. public string NumType { get; set; }
    15. public string LocationCode { get; set; }
    16. }
  2. set the BindingContext for current page(MainPage.xaml) and set ItemsSource for the ListView.

    1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
    2. &lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
    3. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
    4. xmlns:local=&quot;clr-namespace:MauiListViewApp&quot;
    5. x:Class=&quot;MauiListViewApp.MainPage&quot;&gt;
    6. &lt;ContentPage.BindingContext&gt;
    7. &lt;local:MyViewModel&gt;&lt;/local:MyViewModel&gt;
    8. &lt;/ContentPage.BindingContext&gt;
    9. &lt;VerticalStackLayout
    10. Spacing=&quot;25&quot;
    11. Padding=&quot;30,0&quot;
    12. VerticalOptions=&quot;Center&quot;&gt;
    13. &lt;ListView ItemsSource=&quot;{Binding Items}&quot; &gt;
    14. &lt;ListView.ItemTemplate&gt;
    15. &lt;DataTemplate&gt;
    16. &lt;TextCell Text=&quot;{Binding NumType}&quot;
    17. Detail=&quot;{Binding LocationCode}&quot; /&gt;
    18. &lt;/DataTemplate&gt;
    19. &lt;/ListView.ItemTemplate&gt;
    20. &lt;/ListView&gt;
    21. &lt;/VerticalStackLayout&gt;
    22. &lt;/ContentPage&gt;

huangapple
  • 本文由 发表于 2023年2月19日 05:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定