This is a migrated thread and some comments may be shown as answers.

SelectedItemsBehavior fixes for crashes, changing DataContext

7 Answers 258 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jan
Top achievements
Rank 1
Jan asked on 01 Jan 2015, 08:37 AM
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

7 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 01 Jan 2015, 04:03 PM
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.

 
0
Jan
Top achievements
Rank 1
answered on 01 Jan 2015, 07:57 PM
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
0
Jan
Top achievements
Rank 1
answered on 01 Jan 2015, 07:59 PM
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
0
Dimitrina
Telerik team
answered on 02 Jan 2015, 08:25 AM
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.

 
0
Azhar
Top achievements
Rank 1
answered on 27 Jan 2015, 11:19 AM
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])
0
amine boussema
Top achievements
Rank 1
answered on 17 Feb 2016, 04:04 PM

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;        

     }        

 }

0
Stefan
Telerik team
answered on 22 Feb 2016, 01:49 PM
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
Tags
GridView
Asked by
Jan
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Jan
Top achievements
Rank 1
Azhar
Top achievements
Rank 1
amine boussema
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or