SelectedItems and MVVM

7 posts, 0 answers
  1. Ludwig
    Ludwig avatar
    22 posts
    Member since:
    Feb 2011

    Posted 04 Mar 2011 Link to this post

    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
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 Mar 2011 Link to this post

    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!
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ludwig
    Ludwig avatar
    22 posts
    Member since:
    Feb 2011

    Posted 07 Mar 2011 Link to this post

    I tried it, but how can I get notified when the selection changes (without using code behind)?
  5. Brennan
    Brennan avatar
    39 posts
    Member since:
    Sep 2010

    Posted 07 Oct Link to this post

    I'm curiou as to why people keep posting this blog as an answer.  It doesn't work at all.
  6. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 11 Oct Link to this post

    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.
  7. Brennan
    Brennan avatar
    39 posts
    Member since:
    Sep 2010

    Posted 11 Oct in reply to Dilyan Traykov Link to this post

    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,

Back to Top
UI for WPF is Visual Studio 2017 Ready