关于WPF中的Data Virtualizatoin,WPF: Data Virtualization是一篇很好的文章。
使用它,Data Virtualization在我的代码中执行得很好,但是存在一个问题,即我无法将ViewModel中的属性与View中的ItemsControl的SelectedItem绑定。如果一个数据项在数据加载时满足某些条件,则一个项目将在ViewModel中设置为属性,然后它将与View中的ItemsControl的SelectedItem绑定,但不会。
我的代码如下。关于IItemsProvider和VirtualizationCollection的类型,请参阅WPF: Data Virtualization。
到目前为止,我已经尝试过:
实施IItemsProvider的代码
public class WordViewModelProvider : IItemsProvider<WordViewModel>
{
private string _searchText = "some text";
public WordViewModel SelectedItem
{
get;
private set;
}
#region IItemsProvider<WordViewModel> Members
public int FetchCount()
{
lock (_words)
{
int count = (from word in _words
where word.Name.Contains(_searchText)
select word).Count();
return count;
}
}
public IList<WordViewModel> FetchRange(int startIndex, int count)
{
lock (_words)
{
//Please, regard _word as IEnumerable<Word>
IQueryable<Word> query = (from word in _words
where word.Name.Contains(_searchText)
select word);
List<WordViewModel> result = query.ToList().ConvertAll(w =>
{
var wordViewModel = new WordViewModel(w, _searchText);
if (w.Name.Equals(_searchText, StringComparison.InvariantCultureIgnoreCase))
{
SelectedItem = wordViewModel;
}
return wordViewModel;
});
return result;
}
}
#endregion
}
在ViewModel中使用VirtualizingCollection的代码
public void ViewList()
{
var wordViewModelProvider = new WordViewModelProvider();
var virtualizingCollection = new VirtualizingCollection<WordViewModel>(wordViewModelProvider);
//IList<WordViewModel> type to bind with View's ItemsSource.
WordViewModels = virtualizingCollection;
//WordViewModel type to bind with View's SelectedItem
SelectedItem = wordViewModelProvider.SelectedItem;
}
答案 0 :(得分:3)
我想发布关于虚拟化的好参考资料来处理WPF中的大数据集。
虚拟化方法: