ListView BindableSelection suggestion

May 29, 2012 at 2:46 PM

Hi

It can be useful to have existing desired selected items to be respected when attaching.

 

        private void Attach(ListViewBase listView, ObservableCollection<object> boundSelection)        {         

  _listView = listView;          

_listView.Unloaded += OnListViewUnloaded;         

_listView.SelectionChanged += OnListViewSelectionChanged;            _listView.SelectedItems.Clear();         

 _boundSelection = boundSelection;         

 foreach (var o in boundSelection)      
{            
  _listView.SelectedItems.Add(o);         
  }            

_boundSelection.CollectionChanged += OnBoundSelectionChanged;     

  }

May 29, 2012 at 3:05 PM

Actually the above is slightly wrong, due to the order of events. Below is better...

 

        private void Attach(ListViewBase listView, ObservableCollection<object> boundSelection)     

  {           

_listView = listView;       

    _listView.Unloaded += OnListViewUnloaded;       

    _boundSelection = boundSelection;           

_listView.SelectedItems.Clear();       

    foreach (var o in boundSelection)         

  {             

  _listView.SelectedItems.Add(o);         

  }   

        _listView.SelectionChanged += OnListViewSelectionChanged;     

      _boundSelection.CollectionChanged += OnBoundSelectionChanged;   

    }

Coordinator
May 29, 2012 at 6:08 PM

Thanks, I'll check the code. It might wait until the next Win8 build is out though. I considered something like that at first, but decided both lists would usually be empty at first. It seems like you found an example to counter that assumption.