1

After I load the list of customers at a location, I want to select a particular customer as the current item. When I try to move to the current item, no item is selected. I have verified (through debugging) that the item I want to move to exists.

If I move to the current item

(CustomerList.MoveCurrentTo(CustomerList.CurrentItem) //it works but is senseless.
CustomerList is an ICollectionView.

My Question is "How do I move the current item focus to a specific item I choose?".

CustomerList = new CollectionView ( _service.GetCustomers().Where(l => l.LocationID == customer.LocationID));
var item = CustomerList.Cast<AlarmPermit.Entities.Customer>().Where(l => l.BRKey == 52151).Single();
CustomerList.MoveCurrentTo(item);
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Ken
  • 57
  • 1
  • 11
  • 1)Set a breakpoint on the line that begins `var item = CustomerList.Cast...` and see if you are getting the item you expect. If you are, then the problem is in your `CustomerList.MoveCurrentTo(item)`, which isn't shown. 2) You are creating a new instance of `CustomerList` ever time you run this code. Whatever control you are using in your UI is probably bound to an existing "CustomerList", which isn't the one you are using. – Dave Smash Feb 03 '17 at 19:58
  • @ElementalPete I did set a breakpoint, and I am getting the item I'm expecting. I'm not sure why you can't see my CustomerList.MoveCurrentTo(item) statement. It is there. This code is in the constructor of the view model. So this is only executed once. I have a message box showing the item in the CurrentChanged event to see what I'm getting. I get a null item as the current item. – Ken Feb 03 '17 at 20:32
  • I can see where you call `CustomerList.MoveCurrentTo(item)`, I just can't see what that method does. If `item` is not null, and you pass `item` to the `MoveCurrentTo` method, and the `MoveCurrentTo` method is responsible for setting the current item, and the current item is not being set, then the problem is most likely in the `MoveCurrentTo` method... – Dave Smash Feb 03 '17 at 20:42
  • MoveCurrentTo is not my method. It is a part of the ICollectionView interface. – Ken Feb 03 '17 at 21:06
  • Oh, OK, gotcha. In that case, my second guess would be that your computer is haunted. – Dave Smash Feb 03 '17 at 21:09
  • I guess I need to hire an excorcist! LOL! – Ken Feb 03 '17 at 21:11
  • According to the MSDN docs, you may have to approach this in a different manner: https://msdn.microsoft.com/en-us/library/system.windows.data.collectionview(v=vs.110).aspx?f=255&mspperror=-2147217396#Anchor_8 – R. Richards Feb 03 '17 at 21:14

1 Answers1

1

The problem appears to be that I used the CollectionView instead of a class that derives from that class. (Thank you @R.Richards). The MSDN documentation indicates CollectionView should not be used directly.

After I changed the collection from CollectionView to ListCollectionView, the code worked. The CustomerList is still an ICollectionView, but I'm assigning the ListCollectionView object to it.

Here is the modified code:

CustomerList = new ListCollectionView ( _service.GetCustomers().Where(l => l.LocationID == customer.LocationID).ToList());
var item = CustomerList.Cast<AlarmPermit.Entities.Customer>().Where(l => l.BRKey == 52151).Single();
CustomerList.MoveCurrentTo(item);
Ken
  • 57
  • 1
  • 11