Home > DeveloperSection > Forums > CollectionViewSource CurrentItem
Simons Hood
Simons Hood

Total Post:70

Posted on    September-23-2013 7:54 AM


 1 Reply(s)
 2530  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?

Pravesh Singh

Total Post:412

Posted on    September-23-2013 8:23 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick