locked
Selection changed event triggered while updating an Item

    Question

  • Hi ,

        When I update an item i.e get and set an Item in dataSource, Selection changed is getting triggered with e->AddedItems holding that Item and e->RemovedItems holding the same Item .

    How to avoid this selection changed event getting triggered in this case ?

    Thanks

    Thursday, January 3, 2013 8:35 AM

Answers

  • Whenever I _programmatically_ [e.g. in a ViewModel] start 'manipulating' the source collection of business objects bound to a ListView, GridView, etc., I _systematically_ bracket these operations with a [SelectionChanged event handler remove ... add] statement. I do that whatever the complexity of the scenario, so that this event-less state not only avoids disturbing me but also boosts performance since there is no one listening.

    So my approach is:

    - I always listen for the events
    - I 'kind of disable' them using the said technique - i.e. by not listening to them - whenever programmatically changing the list.

    I understand that alternative design concepts can be considered. 

    • Proposed as answer by Jesse Jiang Tuesday, January 8, 2013 7:56 AM
    • Marked as answer by Jesse Jiang Wednesday, January 9, 2013 2:07 AM
    Thursday, January 3, 2013 2:36 PM

All replies

  • I assume that the item is visually selected when you decide to perform this scenario.

    Upon removing then inserting back that item in a list, bracket this operation with a statement that removes then one that adds back the SelectionChanged event handler.

    Thursday, January 3, 2013 11:10 AM
  • Ya , It is visually selected ,

    If it is not visually selected , selection changed gets triggered two times :

    1. with e->AddedItems holding that item

    2. with e->RemovedItems holding that item

    where as when it is visually selected , selectionchanged event gets triggered only once with e->AddedItems and e->RemovedItems holding same item.

    cant I avoid this Selection changed event get triggering at this moment ?

    And also I am just getting that item and setting it back to DataSource , does it cause a remove and Insert (by design) ?

    Thursday, January 3, 2013 1:58 PM
  • Whenever I _programmatically_ [e.g. in a ViewModel] start 'manipulating' the source collection of business objects bound to a ListView, GridView, etc., I _systematically_ bracket these operations with a [SelectionChanged event handler remove ... add] statement. I do that whatever the complexity of the scenario, so that this event-less state not only avoids disturbing me but also boosts performance since there is no one listening.

    So my approach is:

    - I always listen for the events
    - I 'kind of disable' them using the said technique - i.e. by not listening to them - whenever programmatically changing the list.

    I understand that alternative design concepts can be considered. 

    • Proposed as answer by Jesse Jiang Tuesday, January 8, 2013 7:56 AM
    • Marked as answer by Jesse Jiang Wednesday, January 9, 2013 2:07 AM
    Thursday, January 3, 2013 2:36 PM