简体   繁体   中英

Using Data Virtualization, the problem of binding a property in ViewModel with SelectedItem of ItemsControl in View

About Data Virtualizatoin in WPF, the WPF: Data Virtualization is a good article.

With using this, Data Virtualization was executed as good in my code but there is the one problem, which is that I cannot bind a property in ViewModel with SelectedItem of ItemsControl in View. If one item of data satisfies some condition while data loads, the one item will be set as a property in ViewModel and then it will be bound with SelectedItem of ItemsControl in View, but will not.

My code about this is the following. About the types of IItemsProvider andVirtualizingCollection, please refer to the WPF: Data Virtualization .

So far, I have tried:

  1. I'm sure that if Data Virtualization were not used, the Selected Item Binding would be cool.
  2. The IndexOf(T item) method in VirtualizingCollection returns always -1. As thinking this would be the problem, I implemented that the IndexOf(T item) returns a actual index, but it was not concerned with this problem.

The code of implementing 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
}

The code of using VirtualizingCollection in ViewModel

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;
}

I would like to post good references about Virtualization to deal with large data set in WPF.

For Virtualization approaches:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM