[英]Listview doesn't update when the ObservableCollection its bound to changes
[英]ListView not updating when the bound ObservableCollection changes
我正在使用一個搜索窗口,它將搜索結果加載到ObservableCollection中,然后使用ListView顯示結果。
搜索完成后,將ListView的ItemSource設置為ObservableCollection可以正確填充列表。
我正在嘗試更新ListView,因為搜索添加了其他結果,但是ListView根本沒有填充任何數據。 我無法解決我的綁定丟失的地方。
我的研究顯示了使用DataContext的各種方法,盡管似乎沒有幫助。 我嘗試使用CodeBehind以及xaml Window級別將其分配給“ 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));
}
}
我感謝您可以提供的任何幫助,這已經困擾了我好幾天了。
在xaml中設置數據綁定時,將綁定應用程序啟動時存在的ObservableCollection實例。 因此,請務必在應用啟動之前實例化一個實例,並且不要用新實例替換它,除非您在后面的代碼中重置了數據綁定。 如果需要清除其元素,請使用Clear方法。
問題是您使用的是內部實現INotifyCollectionChanged的 ObservableCollection 。 這並不會引起對收藏的任何改變。 僅當添加或從集合中刪除項目時,它才會引發集合更改。
因此,如果有人分配一個新的collection實例( 視您的情況 ),將會發生問題。 因此重置綁定不是一個很好的選擇,而是可以自己進行更改。 通過簡單地實現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或新實例也將反映到綁定控件中。 (您已經有了NotifyStaticPropertyChanged,您只需要創建一個完整的屬性,並在需要時提出更改即可。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.