首页 > 解决方案 > 如何使用 AdvancedCollectionView 源更新 UWP ListView 中的项目

问题描述

我使用Windows Community Toolkit 中的AdvanceCollectionView作为 XAML ListView 的源,以允许排序和筛选。我在更新 ListView 时遇到问题。

为了复制这个问题,我创建了一个简单的Person类。在MainPageXAML 中,我有一个ListView MyXAMLList和一个Button EditButton. 在MainPage代码中,我有一个ObservableCollection<Person> MyPersonListand AdvancedCollectionView MyPersonACV。如果Page_Loaded我将一个人添加到列表中并使用 AdvancedCollectionView 作为列表视图的来源:

Person p = new Person
{
    Name = "John",
    Age = 35
};

MyPersonList.Add(p);
MyPersonACV = new AdvancedCollectionView(MyPersonList, true);
MyXAMLList.ItemsSource = MyPersonACV;    

这行得通,我可以在列表中看到 John。

EditButton代码中,我尝试更新列表中的项目,但这不起作用。ObservableCollection 和 AdvancedCollectionView 都已更新,但 XAML 列表仍显示旧名称“John”而不是“Mary”。

MyPersonList[0].Name = "Mary";

Debug.WriteLine(MyPersonList[0].ToString());
Debug.WriteLine(MyPersonACV[0].ToString());
        

我尝试更新 MyXAMLList.SelectedItem ,但结果相同:

Person p = (Person)MyXAMLList.SelectedItem;
p.Name = "Mary";

我也尝试添加MyPersonACV.Refresh();但没有帮助。

我究竟做错了什么?如何更新列表中的项目?

完整代码如下

人物类:

class Person
{
    public string Name {get; set;}
    public int Age { get; set; }
    public override string ToString()
    {
        return Name;
    }
}

主页 XAML:

<Page
    x:Class="App3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    Loaded="Page_Loaded">

    <Grid>
        <StackPanel Orientation="Vertical">
            <ListView Height="Auto" Width="Auto" x:Name="MyXAMLList" SelectionMode="Single" IsItemClickEnabled="True"/>
            <StackPanel Orientation="Horizontal">
                <Button x:Name="EditButton" Content="Edit" Click="EditButton_Click"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Page>

主页cs:

using Microsoft.Toolkit.Uwp.UI;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


namespace App3
{

    public sealed partial class MainPage : Page
    {

        private ObservableCollection<Person> MyPersonList = new ObservableCollection<Person>();
        private AdvancedCollectionView MyPersonACV;

        public MainPage()
        {
            this.InitializeComponent();
        }


        private void EditButton_Click(object sender, RoutedEventArgs e)
        {

            //Change name
            MyPersonList[0].Name = "Mary";
            //Person p = (Person)MyXAMLList.SelectedItem;
            //p.Name = "Mary";

            Debug.WriteLine(MyPersonList[0].ToString());
            Debug.WriteLine(MyPersonACV[0].ToString());

            //MyPersonACV.Refresh();

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
           //create person 
           Person p = new Person
           {
               Name = "John",
               Age = 35
           };
           //add to list
           MyPersonList.Add(p);
           
           //set up ListView source
           MyPersonACV = new AdvancedCollectionView(MyPersonList, true);
           MyXAMLList.ItemsSource = MyPersonACV;
        }
        

    }
}

标签: c#xamllistviewuwpwindows-community-toolkit

解决方案


我注意到您重写了ToString()显示 ListView 的每个项目的方法。更新 Name 属性时,即使 Name 属性的值更新了,由于 Name 属性和 ListViewItem 没有绑定关系,并且更新数据时不会触发 ToString() 方法,所以 UI 不会更新. 最好使用DataTemplate自定义项目的外观,将 Name 属性绑定到元素(例如 TetxBlock)并实现INotifyPropertyChanged接口。在这种情况下,当 Name 属性发生更改时,它将向绑定提供更改通知,并且 UI 将更新。例如:

.xaml:

<ListView Height="Auto" Width="Auto" x:Name="MyXAMLList" SelectionMode="Single" IsItemClickEnabled="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

。CS:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private string name { get; set; }
    public string Name 
    {
        get 
        {
            return name;
        }
        set 
        {
            name = value;
            OnPropertyChanged();
        }
    }
    public int Age { get; set; }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

private void EditButton_Click(object sender, RoutedEventArgs e)
{
    //Change name
    MyPersonList[0].Name = "Mary";
}

推荐阅读