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

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

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

问题

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

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

  1. 仅使用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:

  1. XAML only approach.
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             x:Class=&quot;...&quot;
             x:Name=&quot;page&quot;
             xmlns:services=&quot;...&quot;
             Title=&quot;...&quot;&gt;

  &lt;ListView BindingContext=&quot;{x:Reference page}&quot; ItemsSource=&quot;{Binding observableCollection}&quot;&gt;
  &lt;/ListView&gt;

&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:

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

Code-Behind File:

public partial class ContentPage 
{
  // Edit: no need for a setter here
  public ObservableCollection&lt;type&gt; 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)

您可以参考以下代码:

  1. 创建一个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; }
}
  1. 为当前页面(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:

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

    public class MyViewModel {
        public ObservableCollection&lt;Item&gt; Items { get; set; }
    
        public MyViewModel() {
            Items = new ObservableCollection&lt;Item&gt;();
    
            Items.Add(new Item { NumType = &quot;S&quot;, LocationCode = &quot;0001&quot; });
            Items.Add(new Item { NumType = &quot;M&quot;, LocationCode = &quot;0002&quot; });
            Items.Add(new Item { NumType = &quot;L&quot;, LocationCode = &quot;0003&quot; });
            Items.Add(new Item { NumType = &quot;S&quot;, LocationCode = &quot;0001&quot; });
            Items.Add(new Item { NumType = &quot;M&quot;, LocationCode = &quot;0002&quot; });
            Items.Add(new Item { NumType = &quot;L&quot;, LocationCode = &quot;0003&quot; });
    
        }
    }
    
    public class Item {
    
        public string NumType { get; set; }
    
        public string LocationCode { get; set; }
    
    }
    
  2. set the BindingContext for current page(MainPage.xaml) and set ItemsSource for the ListView.

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; 
    
    &lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
              xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
              xmlns:local=&quot;clr-namespace:MauiListViewApp&quot;
              x:Class=&quot;MauiListViewApp.MainPage&quot;&gt;
    
        &lt;ContentPage.BindingContext&gt;
             &lt;local:MyViewModel&gt;&lt;/local:MyViewModel&gt;
        &lt;/ContentPage.BindingContext&gt;
    
        &lt;VerticalStackLayout
             Spacing=&quot;25&quot;
             Padding=&quot;30,0&quot;
             VerticalOptions=&quot;Center&quot;&gt;
    
            &lt;ListView ItemsSource=&quot;{Binding Items}&quot; &gt;
                &lt;ListView.ItemTemplate&gt;
                    &lt;DataTemplate&gt;
                        &lt;TextCell Text=&quot;{Binding NumType}&quot; 
                                  Detail=&quot;{Binding LocationCode}&quot; /&gt;
                    &lt;/DataTemplate&gt;
                &lt;/ListView.ItemTemplate&gt;
            &lt;/ListView&gt;
    
         &lt;/VerticalStackLayout&gt;
    
     &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:

确定