CollectionViewSource CurrentItem

Posted by  Simons Hood
 3437  View(s)
Rate this:

I am using a CollectionViewSource in a dialog view model that has various filtering requirements, which works fine. I also maintain the equivalent of the selected item in a property (SelectedProject), and I'm wondering if I could / should do away with it since the View will know the current item. My data binding looks like:


      ItemsSource="{Binding Projects.View}"


      SelectedItem="{Binding SelectedProject, Mode=TwoWay}">

I use the setter for the SelectedProject to facilitate unit testing, and the CurrentItem doesn't appear to be settable as far as I can see. I also need to cast it to the right object when I want to use it. OTOH, if the SelectedProject is redundant then I'd show it the same respect as any other redundancy and delete it.

So, how do you typically deal with the current item when you're using a CollectionViewSource?

  1. Re: CollectionViewSource CurrentItem

    Hi Samuel,

    You could do away with the SelectedProject, but I would argue against it. If you have the property in your code, it is clear what you are doing. If you don't have it, you will need to do something like

    CollectionViewSource.GetDefaultView(Projects.View).CurrentItem as Project

    just to interact with the current project. I value clarity over "built-in". On top of that, CurrentItem is read-only, so if you ever want to select an item in the ViewModel, it wouldn't be possible.

      Modified On Mar-31-2018 01:38:48 AM