COLLECTIONVIEWSOURCE CURRENTITEM

Simons Hood

Total Post:70

Points:494
Posted by  Simons Hood
WPF
 2551  View(s)
Ratings:
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:

<ListView 

      ItemsSource="{Binding Projects.View}"

      IsSynchronizedWithCurrentItem="True"

      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. Pravesh Singh

    Post:412

    Points:2888
    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!