I am trying to implement multiple selection of a RadGridView in an MVVM context with data binding, based on this example.
I have achieved one way of interaction: when the selection state in the grid view is changed, the SelectedItems property of the view model is changed accordingly.
However, when I try to update the view from the viewmodels state, I run into a loop, where a change in the view changes the view model, which tries to update the view again etc. as a consequence, WPF throws an exception, that a change of a collection inside a CollectionChanged event is not possible (which makes sense).
Any ideas how this is solved properly ? Unfortunately, the above mentioned example does not present any further details for this issue.
8 Answers, 1 is accepted
I have prepared a sample project in order to show you how the Binding to multiple selection could be implemented. I have one RadGridView and I have added a button to remove one of the items in the SelectedItems collection Property. By pressing the button you may see that when the Property in the ViewModel has changed, the GridView is updated accordingly.
Please let me know if you have any further problems with the data binding.
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
thanks for the sample, that made my day. The solution was much easier than what I tried to build after reading the blog entry mentioned.
This approach should probably go into the documentation for the RadGridView itself.
Thank you for your feedback. We will consider adding a new 'How To' article into the documentation.
the Telerik team
I have followed your example but since I am using the MVVMLight toolkit I do not explicitly bind my view model in the view as the data context but relay on the ViewModelLocator to do that. It appears as if the OnAttached event fires before the this completes as I get a NullReferenceException on the this.AssociatedObject.DataContext part. Any ideas how to resolve other that explicitly binding the data context?
Good Question, i have the same Problem
I used your solution to keep the SelectedItems colleciton in sync with my ViewModels property, and it works, as far as the collection is concerned.
But changing the selection in the ViewModel (first clearing the collection and then adding items) does not produce the expected visual result. The SelectedItems colleciton of the GridView is correct, but sometimes only the first item is highlighted.
Maybe it is because I am creating new Items, adding them to the collection which ist bound as ItemsSource and then add them to the SelectedItems. But once again: the SelectedItems collection is always correct, in the ViewModel and in the GridView. It is just the visual feedback, which is not as expected.
Any ideas what is going wrong?
You can check this github example on how to bind the SelectedItems to a collection from your view model.
Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples developing our brand new SDK Samples Browser. You can also use it to review the examples.
I hope this approach will work for you.
when using multiple binding in WPF on two property that merge in one column binding searh dose not work
please give me a solution