SelectedItemsBehavior fixes for crashes, changing DataContext

8 posts, 0 answers
  1. Jan
    Jan avatar
    7 posts
    Member since:
    Aug 2010

    Posted 01 Jan 2015 Link to this post

    Telerik provide a SelectedItemsBehavior example for two-way binding of SelectedItems. It is on GitHub here:
    https://github.com/telerik/xaml-sdk/tree/master/GridView/BindingSelectedItemsFromViewModel

    Original blog post from 2010 here: http://blogs.telerik.com/vladimirenchev/posts/10-05-31/how-to-synchronize-your-ui-selected-items-with-your-data-context-using-mvvm-and-blend-behaviors-for-silverlight-and-wpf.aspx

    In my experience, and as others have reported, it is a little buggy:
    1. It will crash in the Transfer() method intermittently. 
    For example, this was reported by another user: I used the solution suggested in the last comment by Vlad from github, it worked fine, but sometimes I am getting the error "cannot change observablecollection during a collectionchanged event". I also get this error.

    2. If you change the DataContext, then the SelectedItems collection no longer gets updated.

    I have developed some very simple fixes and now it works beautifully. First of all, we only subscribe to events once. Second, we only attach ourselves to the grid once.

    Here is a pull request - maybe the mods will merge it in:
    https://github.com/HolbergEEG/xaml-sdk/commit/30ae7a8ebe0513674921f72b3dff1d4332dd63a9

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 01 Jan 2015 Link to this post

    Hi,

    Thank you for submitting your suggestion. I tested the change modifying the actual project, however, once I tried selecting an additional item, it crashed. You can check the attached image. Have you missed to commit any additional change I need to do?

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Jan
    Jan avatar
    7 posts
    Member since:
    Aug 2010

    Posted 01 Jan 2015 Link to this post

    Thanks for a very rapid reply on the first day of 2015!

    I'm sorry, the code I submitted was incomplete. I fixed it in my own code, then "backported" into your xaml-sdk project by hand and there were some lines missing. I updated the pull request on GitHub with a new committ. It should work now. The key is to make sure the events are subscribed to only once, and only attaching the behavior to the RadGrid once.

    I'd be very happy if you could verify that it does work in your hands.

    Yours,

    Jan
  5. Jan
    Jan avatar
    7 posts
    Member since:
    Aug 2010

    Posted 01 Jan 2015 Link to this post

    Thanks for a very rapid reply on the first day of 2015!

    It is fixed now. The pull request has been updated with a new commit. I fixed it in my own code, then manually "backported" it into your xaml-sdk code base, but forgot a few lines. The key is to make sure you don't subscribe to events twice, and to attach the behavior only once.

    Could you verify that it works in your hands?

    Yours,

    Jan
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 02 Jan 2015 Link to this post

    Hello Jan,

    Thank you! It works fine now and I updated the code in our solution also. In addition, I updated your Telerik points accordingly.

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Svenn
    Svenn avatar
    5 posts
    Member since:
    Dec 2012

    Posted 27 Jan 2015 Link to this post

    Update: If you use the fix for SelectedItemsBehavior on different controls, it will not work. There is only one static member IsAttached, and it should depend on the attachment status of different controls.

    Very busy, can't commit, but this is a placeholder until I can push it:

    Line 15: private static Dictionary<DependencyObject, Boolean> isAttachedDictionary = new Dictionary<DependencyObject, bool>();
    Line 40: if (grid != null && selectedItems != null && isAttachedDictionary.ContainsKey(dependencyObject) && !isAttachedDictionary[dependencyObject])
  8. amine boussema
    amine boussema avatar
    4 posts
    Member since:
    Sep 2009

    Posted 17 Feb in reply to Svenn Link to this post

    Hi

    the method needs to be like this in order to work : 

     

    private static void OnSelectedItemsPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)         {            

     RadGridView grid = dependencyObject as RadGridView;             INotifyCollectionChanged selectedItems = e.NewValue as INotifyCollectionChanged;

                if (!isAttachedDictionary.ContainsKey(grid))             {                 

    isAttachedDictionary.Add(grid, false);         

        }             if (grid != null && selectedItems != null && isAttachedDictionary.ContainsKey(dependencyObject) && !isAttachedDictionary[dependencyObject])             {                 MySelectedItemsBindingBehavior behavior = new MySelectedItemsBindingBehavior(grid, selectedItems);                 behavior.Attach();                 isAttachedDictionary[dependencyObject] = true;        

         }        

     }

  9. Stefan X1
    Admin
    Stefan X1 avatar
    518 posts

    Posted 22 Feb Link to this post

    Hello Svenn, Amine,

    Thank you for your valuable feedback.

    ​We will update the example according to your suggestion.


    All the best,
    Stefan X1
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready