3

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;
}
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45
  • There is a good article covering this issue, which is the [How can I improve on existing WPF data virtualization solutions?](http://bea.stollnitz.com/blog/?p=378) – Jin-Wook Chung Jul 08 '11 at 10:58
  • That article has been dead for a number of years but I was able to find it on archive.org's way back machine. Here is the link https://web.archive.org/web/20150104001509/http://www.zagstudio.com/blog/378 – tkefauver Jan 13 '21 at 17:05

1 Answers1

2

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

For Virtualization approaches:

Sheridan
  • 68,826
  • 24
  • 143
  • 183
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45
  • 1
    All links to Bea Stollnitz are dead (June 2016). Do you have the info and or the source code somewhere else? Please share it if you do, thanks. – Hannish Jun 10 '16 at 13:24
  • 1
    @Hannish luckily I have implemented this in the past and have the code I wrote at home on my computer somewhere. I need this solution again. Microsoft should really just add a VirtualizedCollection to the WPF library. – Lee O. Oct 31 '16 at 16:07
  • @LeeO. could you please upload it into a DropBox or something similar and post a link for others to download? Thanks! – Hannish Nov 01 '16 at 13:21
  • 1
    @Hannish Discovered my USB is either lost or still packed away as I just moved a month ago. In other words I was unable to find it at this time. I did find a github repo that looks like it's a very good representation of Bea's code. Obviously missing the blog part which was very helpful back when I did this the first time. Here is the repo: https://github.com/lvaleriu/Virtualization/tree/master/DataVirtualization – Lee O. Nov 01 '16 at 18:22
  • @LeeO. Thanks, man! If you ever find the USB with the data please be kind enough to post it, I'm sure it will help a lot of people. Best regards! – Hannish Nov 04 '16 at 09:48
  • 1
    @Hannish, I have replaced the broken Bea Stollnitz link. – Sheridan Nov 15 '17 at 15:43
  • @Sheridan Thank you but please check your links, the one by Vincent Van Den Berghe points to the old Bea Stollnitz broken link. – Hannish Nov 17 '17 at 08:29
  • 1
    @Hannish, I checked the edits and that link always pointed to that URL... I replaced the bea.stollnitz link (the last link). – Sheridan Nov 17 '17 at 09:10