views:

86

answers:

2

Hello,

I will explain my question a little more.

I have a ObservableCollection binded to a ItemsControl with it datatemplate. Nothing new :P.

The thing is that I'm using a Drag & Drop library for changing the order of the items in the ItemsControl. The library works fine, you can drag some items in the ItemsControl.

The problem is that the changes in the ItemsControl doesn't affect to the ObservableCollection.

The question is: Can I save the ItemsControl source to a new ObservableCollection? Or... you have a better idea?

Thank you and I hope that you can understand me and my english :P

A: 

ObservableCollection has a Move method. The method takes in an old index and a new index. When your ItemsControl changes, you can call Move on the bound collection with the old index of the item and the new (current) index. Then the ObservableCollection will be updated.

Charlie
Jesus Rodriguez
I bet the Drag and Drop library has some events you can hook up to. Listen to the item dropped event, and then write some code to get its old index and new index.
Charlie
A: 

Along with Charlie's answer I can add little more towards what you can do to get this done Hopefully the DragDrop library will give you a dropped event which can give you the dragged item(Source) Source.DataContext will be an Item in your ObservableCollection and you can easly find out the OLD_INDEX by Collection.IndexOf(item). Now since your dragdrop library moved the Source.

You can get the NEW_INDEX by ItemsContainerGenerator.IndexFromContainer(source) provided the Source is the ContentControl for the ItemsControl. Or else you might need to walk through the VisualTree to find out the ContentControl which made up the ItemsControl. This depends on the drag-drop library implementation.

Now collection.Move(OLD_INDEX, NEW_INDEX) thats it.

Jobi Joy