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
7 Answers, 1 is accepted
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.
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
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
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.
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])
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;
}
}
Thank you for your valuable feedback.
We will update the example according to your suggestion.
All the best,
Stefan X1
Telerik