3

I have an ItemsControl bound to a sorted (via CollectionViewSource) derived ObservableCollection of view models. In my derived collection class, I'm handling PropertyChanged on my view models. When a certain property (Order) has changed I need the rendering of the controls to reflect the new, sorted collection.

I understand, that in my handling of PropertyChanged, I can call OnCollectionChanged with the Reset action specified; however, this leads to loss of focus on the control, which is undesirable for my situation.

Is there anyway I can handle the change and have a smooth transition with the cursor remaining where it is (or in the case of a tab, move to the next appropriate field).

Tyler
  • 514
  • 2
  • 9
  • Maybe I'm asking the wrong question. Is it proper that I rely on the data binding to update the control order? Or is that my burden to handle? – Tyler May 20 '11 at 21:06

1 Answers1

1

What changes the Order property?

Perhaps you can look into re-arranging your ItemsControl instead of sorting and refreshing your source.

I did this in the past with a TabControl. When a user Drags/Drops a tab into a new location, it removes the actual TabItem from the TabControl and inserts it in the new location. The SelectedTab was never altered so I didn't have to worry about focus changing.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • I dislpay a user control for each item in the collection. When the user changes the Order value via the UI, the item needs to shift to match the change in order. So it's a UI change based on a data change. Does that make more sense? – Tyler May 20 '11 at 19:44
  • @Tyler Is it a seemless transition? For example, does the user enter a value and it automatically updates the UI or do they have to hit a button to save the updates and the UI updates after that? In the 1st case I might look into doing something in the code-behind to reorder the XAML elements, in the 2nd case I would probably try and store the selectedItem and whatever item has focus, re-sort/refresh the list, then put the selected item/focus back. – Rachel May 21 '11 at 16:25
  • It's the first scenario. I'm looking into taking advantage of MVVM and WPFs data binding. This is one scenario in our existing codebase that I couldn't quite figure out. Turns out I was looking in the wrong location. I'll look into handling an appropriate event in the views code-behind. Thanks. – Tyler May 23 '11 at 05:52
  • thanks for this tip. I was shooting into the dark on this one and your suggestion helped me find a quick solution. – jpierson Jun 27 '11 at 12:50
  • @jpierson, Hi could you post your final solution ? – Juan Pablo Gomez May 10 '16 at 03:57
  • @JuanPabloGomez, sorry I'm no longer working for the company where that code was written so I don't have access or the authority to share the result. – jpierson May 10 '16 at 15:52
  • @JuanPabloGomez I'm not sure if it will help or not, but a copy of the code I referenced in my answer about dragging/dropping tab control tabs can be found [here](http://stackoverflow.com/a/3627308/302677). It's not a direct answer to jpierson's answer though, so might not be the kind of thing you were looking for. – Rachel May 10 '16 at 16:01