ListView Drag and Drop Renumber


  • Hi,

    I’m working on a Store App which has a ListView bound to an observable collection of a custom object.    One of the properties on this objects is an int called Position, which fires a change notification event and is bound to a TextBlock in the DataTemplate.   This TextBlock needs to display the Position in the list, so the instance at index 0 in the observable collection displays 1, index 1 displays 2 etc.

    If I drag and drop the items to re-order them, on the collection changed event if the Action is add (first it removes the dragged object and then adds it again) the items in the observable collection are correct and with the following code, the Position property is being set correctly.

    if (e.Action == NotifyCollectionChangedAction.Add)
    for (int i = 0; i < PeopleDtos.Count; i++)
                            PeopleDtos[i].Position = i + 1;

    (I’m also filtering this so it only gets executed after a Drag & Drop)

    When I add a breakpoint and examine the items  in the collection after this code is executed, everything looks fine.  If I place a breakpoint on the Position property setter, it’s being set correctly for all instances.

    But if I have say 3 items in the list and I drag 3 above 2, former item 3 correctly becomes 2, but former Item 2 remains 2, rather than 3.

    I have no idea what I’m missing here and would be grateful for any suggestions.


    Tuesday, August 26, 2014 3:35 AM

All replies

  • Can you post your code?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, August 26, 2014 7:52 PM