in XAML:
<ListBox x:Name="AllJobListBox" MinHeight="200" MinWidth="500" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Path=AllJobList}" >
in CodeBehind:
DataContext = new LoadJobWindowViewModel();
//ctor where ObservableCollection is instantiated and populated
in ViewModel: //bound to textbox on View
public string SearchText {
get {
return searchText;
}
set {
searchText = value;
OnPropertyChanged("SearchText");
}
}
Command:
public void SearchJob()
{
ObservableCollection<Job> filteredJobs = new ObservableCollection<Job>(AllJobList.Where(j => j.JobName.Contains(SearchText)));
AllJobList = filteredJobs;
}
我一直在仔細研究博客和帖子,試圖找出我遺漏了什么或做錯了什么,但無法確定。任何幫助將不勝感激。
uj5u.com熱心網友回復:
確保AllJobList引發INotifyPropertyChanged.PropertyChanged事件。
您當前的解決方案在性能方面非常昂貴。您應該始終避免替換完整的源集合實體,因為它會強制ListBox丟棄所有容器并開始完整的布局傳遞。相反,您想使用 aObservableCollection并對其進行修改。
出于這個原因,您應該始終更喜歡通過集合視圖(ICollectionView)進行過濾和排序。請參閱Microsoft Docs:資料系結概述 - 集合視圖。操作視圖也不需要過濾或排序原始集合:
ICollectionView allJobListView = CollectionViewSource.GetDefaultView(this.AllJobList);
allJobListView.Filter = item => (item as Job).JobName.Contains(this.SearchText);
由于 aBinding始終使用集合的視圖作為源而不是集合本身(請參見上面的鏈接),因此ListBox(或ItemsSource Binding準確地說)將在將 a 分配Predicate<object>給ICollectionView.Filter屬性時自動更新。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/472171.html
上一篇:如何在Stringresources所在的Resources檔案夾中的styles.xaml中系結靜態資源的源?
