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

SelectedItems and MVVM

6 Answers 2668 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ludwig
Top achievements
Rank 1
Ludwig asked on 04 Mar 2011, 08:31 AM
Hi,

small question: how can I databind SelectedItems of a gridview to a collection of my viewmodel? I have a gridview with SelectionMode set to Extended:

<telerik:RadGridView Name="patientsGridView" SelectionMode="Extended" ItemsSource="{Binding Patients}" SelectedItem="{Binding SelectedPatient}" AutoGenerateColumns="False" IsReadOnly="True" >           
 
and the viewmodel would contain:

public ObservableCollection<PatientViewModel> SelectedPatients { ... }

But how can I bind the grid's SelectedItems to the viewmodel's SelectedPatients?

Thanks,
Kind regards,
L

6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 04 Mar 2011, 09:40 AM
Hi,

 You can check this blog post for more info. 

Greetings,
Vlad
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Ludwig
Top achievements
Rank 1
answered on 07 Mar 2011, 04:44 PM
I tried it, but how can I get notified when the selection changes (without using code behind)?
0
Ted
Top achievements
Rank 1
answered on 15 May 2014, 12:56 AM
0
Brennan
Top achievements
Rank 1
answered on 07 Oct 2016, 08:29 PM
I'm curiou as to why people keep posting this blog as an answer.  It doesn't work at all.
0
Dilyan Traykov
Telerik team
answered on 11 Oct 2016, 08:33 AM
Hello Brennan,

For brevity, only part of the full code has been included in the blog post. A working version, however, has been provided at the bottom of the post. For your convenience, I'm attaching it here as well.

You can also find a similar example in the SDK Samples Browser with the title Binding SelectedItems From ViewModel.
 
I hope you find this helpful. Do let me know if you have any other questions or concerns.

Regards,
Dilyan Traykov
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Brennan
Top achievements
Rank 1
answered on 11 Oct 2016, 01:54 PM

Hi Dilyan,

I was actually using the full code example on the blog post and still could not get it to work.  The github SDK example, however, I was able to get working, once I realized the view model SelectedItems property does not have a setter. I was trying to two-way bind the setter, and was expecting my viewmodel to be updated right as the selection in the grid changed, which obviously would not work in this case.  Once I explicitly called the getter on my viewmodel selecteditems collection, I got the expected result.

Thanks,

Pradeep
Top achievements
Rank 1
commented on 25 Nov 2021, 01:09 PM

Hi Brennan,

What do you mean by "Once I explicitly called the getter on my viewmodel selecteditems collection, I got the expected result." Please explain.

 I am trying the two way binding of SelectedItems collection of my viewmodel. Thanks. 
Tags
GridView
Asked by
Ludwig
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Ludwig
Top achievements
Rank 1
Ted
Top achievements
Rank 1
Brennan
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or