C# 如何使用AdvancedCollectionView源更新UWP ListView中的项
我使用Windows社区工具包作为XAML ListView的源,以允许排序和筛选。我在更新ListView时遇到问题 为了复制这个问题,我创建了一个简单的C# 如何使用AdvancedCollectionView源更新UWP ListView中的项,c#,xaml,listview,uwp,windows-community-toolkit,C#,Xaml,Listview,Uwp,Windows Community Toolkit,我使用Windows社区工具包作为XAML ListView的源,以允许排序和筛选。我在更新ListView时遇到问题 为了复制这个问题,我创建了一个简单的Person类。在MainPageXAML中,我有一个ListView MyXAMLList和一个按钮EditButton。在main页面code中,我有一个observeCollection MyPersonList和AdvancedCollectionView MyPersonACV。在加载的Page_事件中,我将一个人添加到列表中,并使
Person
类。在MainPage
XAML中,我有一个ListView MyXAMLList
和一个按钮EditButton
。在main页面
code中,我有一个observeCollection MyPersonList
和AdvancedCollectionView MyPersonACV
。在加载的Page_
事件中,我将一个人添加到列表中,并使用AdvancedCollectionView作为列表视图的源:
Person p = new Person
{
Name = "John",
Age = 35
};
MyPersonList.Add(p);
MyPersonACV = new AdvancedCollectionView(MyPersonList, true);
MyXAMLList.ItemsSource = MyPersonACV;
这很有效,我可以在名单上看到约翰
在edit按钮中
code我尝试更新列表中的项目,但这不起作用。ObservableCollection和AdvancedCollectionView都已更新,但XAML列表仍显示旧名称“John”而不是“Mary”
我尝试更新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;
}
}
}
使用Microsoft.Toolkit.Uwp.UI;
使用System.Collections.ObjectModel;
使用系统诊断;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
名称空间App3
{
公共密封部分类主页面:第页
{
私有ObservableCollection MyPersonList=新ObservableCollection();
私人高级收藏查看我的个人简历;
公共主页()
{
this.InitializeComponent();
}
私有无效编辑按钮\u单击(对象发送方,路由目标)
{
//改名
MyPersonList[0]。Name=“Mary”;
//Person p=(Person)MyXAMLList.SelectedItem;
//p、 Name=“玛丽”;
Debug.WriteLine(MyPersonList[0].ToString());
Debug.WriteLine(MyPersonACV[0].ToString());
//MyPersonACV.Refresh();
}
已加载私有无效页面(对象发送方,路由目标e)
{
//创造人
人员p=新人员
{
Name=“John”,
年龄=35
};
//添加到列表中
MyPersonList.Add(p);
//设置ListView源
MyPersonalCV=新的AdvancedCollectionView(MyPersonList,true);
MyXAMLList.ItemsSource=MyPersonACV;
}
}
}
我注意到您重写了ToString()
方法来显示ListView的每个项目。更新Name属性时,即使Name属性的值已更新,由于Name属性和ListViewItem之间没有绑定关系,并且更新数据时不会触发ToString()方法,因此UI也不会更新。最好使用DataTemplate自定义项的外观,将Name属性绑定到元素(例如TetxBlock)并实现INotifyPropertyChanged接口。在这种情况下,当名称proeprty更改时,它将向绑定提供更改通知,并且UI将更新。对于exmaple:
.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>
谢谢,那确实管用。但是AdvancedCollectionView的文档说我不必使用ObservableCollection,可以使用一个简单的通用列表。我以为ACV会处理这个?“使用AdvancedCollectionView不需要使用例如ObservableCollection。即使在构造函数中提供简单列表,它也能按预期工作。”()
<ListView Height="Auto" Width="Auto" x:Name="MyXAMLList" SelectionMode="Single" IsItemClickEnabled="True">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
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";
}