gpt4 book ai didi

c# - 当绑定(bind)的 ObservableCollection 更改时 ListView 不更新

转载 作者:行者123 更新时间:2023-12-02 04:14:14 25 4
gpt4 key购买 nike

我正在开发一个搜索窗口,它将搜索结果加载到 ObservableCollection 中,然后使用 ListView 显示结果。

搜索完成后将 ListView 的 ItemSource 设置为 ObservableCollection 可以正确填充列表。

我试图让 ListView 在搜索添加其他结果时更新,但 ListView 根本不填充任何数据。我不知道我的绑定(bind)在哪里掉落。

我的研究展示了使用 DataContext 的各种方法,但似乎没有一个有帮助;我尝试使用 CodeBehind 以及 xaml 窗口级别将其分配给“this”和我的 CachedData 类。

抱歉,代码片段很长,我留下了我认为可能有助于为问题添加上下文的任何内容。

XAML:

<Window x:Class="SLX_Interface.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SLX_Interface"
mc:Ignorable="d"
Title="SLX Search" Height="auto" Width="auto">
<Window.CommandBindings>
</Window.CommandBindings>
<Grid>
<Grid.Resources>
<local:CachedData x:Key="cachedData" />
</Grid.Resources>
<TabControl x:Name="tabControl" Grid.RowSpan="2" Margin="0,20,0,0">
<TabItem Header="Accounts" Name="accountsTab">
<Grid>
<ListView x:Name="accountSearchResultsListView" Margin="5,32,5,30" DataContext="staticResource cachedData" ItemsSource="{Binding Path=accounts}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView x:Name="accountSearchResultsGridView">
<GridViewColumn Header="SData Key" DisplayMemberBinding="{Binding SDataKey}"/>
<GridViewColumn Header="Account Name" DisplayMemberBinding="{Binding AccountName}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</TabItem>
</TabControl>
</Grid>

MainWindow.xaml.cs 内的代码隐藏:

private async void SearchAccount(string searchTerm, string searchField, string searchOperator)
{
//Create the string we'll use for searching
string urlString = "Stuff";

//Create an ObservableCollection, then use it to blank the cache
ObservableCollection<Account> resultsList = new ObservableCollection<Account>();
CachedData.accounts = resultsList;

//Getting data from the search using an XML Reader
XmlReader resultsReader = null;

try
{
//Using XmlReader to grab the search results from SLX
XmlUrlResolver resultsResolver = new XmlUrlResolver();
resultsResolver.Credentials = LoginCredentials.userCred;

XmlReaderSettings resultsReaderSettings = new XmlReaderSettings();
resultsReaderSettings.XmlResolver = resultsResolver;
resultsReaderSettings.Async = true;

resultsReader = XmlReader.Create(urlString, resultsReaderSettings);
}
catch (Exception error)
{

}

//Grabbing data from the XML and storing it, hopefully updating the ListView as we go
using (resultsReader)
{
while (await resultsReader.ReadAsync())
{
while (resultsReader.ReadToFollowing("slx:Account"))
{
//Setting data from the XML to a new Account object ready to be passed to the list
Account account = new Account();
account.GUID = new Guid();

resultsReader.MoveToFirstAttribute(); account.SDataKey = resultsReader.Value;
resultsReader.ReadToFollowing("slx:AccountName"); account.AccountName = resultsReader.ReadElementContentAsString();

CachedData.accounts.Add(account);

//--Uncommenting this gives odd results;
//--The first item is displayed, any others aren't.
//--If there are a lot of items, the application eventually errors like mad and ends.
//--Looks like one error window for each item, though I don't see the message before they die along with the application.
//accountSearchResultsListView.ItemsSource = CachedData.accounts;
}
}
}

//--Uncommenting this works but shows the data once the entire XML has been read through, which can take some time so isn't ideal.
//accountSearchResultsListView.ItemsSource = CachedData.accounts; }

上面的类引用,存储在单独的 .cs 文件中,但位于同一命名空间下:

public class CachedData
{
public static ObservableCollection<Account> accounts { get; set; }

public static event PropertyChangedEventHandler PropertyChanged;

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged = delegate { };
private static void NotifyStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
}
}

public class Account : IEquatable<Account>
{
public Guid GUID { get; set; }
public string SDataKey { get; set; }
public string AccountName { get; set; }

public override string ToString()
{
return AccountName;
}

public override bool Equals(object obj)
{
if (obj == null) return false;
Account objAsPart = obj as Account;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}

public override int GetHashCode()
{
return 0;
}

public bool Equals(Account other)
{
if (other == null) return false;
return (GUID.Equals(other.GUID));
}
}

感谢您提供的任何帮助,这已经困扰了我好几天了。

最佳答案

问题是您正在使用ObservableCollection,它在内部实现INotifyCollectionChanged。这不会引发对集合的所有更改。仅当在集合中添加或删除项目时,它才会引发集合更改。

所以问题就出现了,如果有人分配一个新的集合实例(根据您的情况)会发生什么。因此,重置绑定(bind)并不是一个很好的选择,而是您可以自己进行更改。通过简单地实现 INotifyPropertyChanged。(通常情况)

public class DataClass : INotifyPropertyChanged
{

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}


private ObservableCollection<string> collection;

public ObservableCollection<string> Collection
{
get { return collection; }
set
{
collection = value;
OnPropertyChanged("Collection");
}
}
}

因此将集合分配给null或新实例也会反射(reflect)到绑定(bind)控件。 (您已经拥有 NotifyStaticPropertyChanged,您只需要创建一个完整的属性并在需要时引发更改。)

关于c# - 当绑定(bind)的 ObservableCollection 更改时 ListView 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34829368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com