CollectionViewSource CurrentItem
Asked Answered
B

3

10

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?

Board answered 27/2, 2010 at 19:4 Comment(0)
L
7

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.

Lanalanae answered 28/2, 2010 at 15:3 Comment(2)
You can set the current item with various Move methods but I agree the readability is worth the arguable redundancy. CheersBoard
That last point is incorrect. CollectionView.MoveCurrentTo(object item) will allow you to update the selection in the view model.Defray
S
1

You need to understand that the SelectedItem from the ListView is independent of the ItemsSource. Whether you use a CollectionViewSource or a List or an Array, the selected item will alway represent the item of that collection.

So to answer your question, as to why your SelectedProject is not setting, I suggest you check your setter functionality for errors. A good way to find out if the binding contains any errors is to check our Output for any binding error messages during debug.

NOTE: If your SelectedProject is the same type as the items in your Projects CollectionViewSource, then you needn't to cast it before usage (unless you have made SelectedProject of Object type then that also explains your setting problem).

EDIT: Sorry, short answer is no, it is not redundant. Having a variable bound to the current item is not redundant if you have testing in mind. A good example is when you want to test an old version of the SelectedItem with the new. Now if you only refer to the CollectionViewSource's SelectedItem, then it may be too late to compare, but with your own variable, you can test the logic before you set it again.

Spleenwort answered 28/2, 2010 at 5:38 Comment(1)
The SelectedProject works perfectly - the question was whether it's redundant to the ColloctionViewSource CurrentItem.Board
W
1

In my case, I was trying to set the SelectedItem on a ListBox whos ItemsSource was bound to a CollectionViewSource. It is messy because you really need to set the current item on the CollectionViewSource and not the ListBox.SelectedItem....so I created a Extension method to handle this for scenario for me:

YourListBox.SetCurrentItemOnView<YourObjectType>(item);

...and the Extension method definition

public static void SetCurrentItemOnView<T>(this System.Windows.Controls.ListBox listBox, T item) where T : YourObjectType
{
    var view = listBox.ItemsSource as ListCollectionView;
    if (view == null) { return; }

    var itemToSelect = (from p in view.SourceCollection.OfType<T>()
                        where p.ID == item.ID
                        select p).FirstOrDefault();

    view.MoveCurrentTo(itemToSelect);
}

I'm guessing this was an issue for me because in my case the item reference I had was not the same as the item reference contained in the ItemsSource, so I had to resolve it like this.

Warehouseman answered 18/9, 2013 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.