Child RadGrid set SelectedItem on ViewModel MVVM

6 posts, 0 answers
  1. David
    David avatar
    3 posts
    Member since:
    Jul 2012

    Posted 12 Jul 2012 Link to this post

    I am currently using the solution outlined this post 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 

    to set my SelectedItem and SelectedItems from a RadGrid to a property on my viewModel (this works great by the way -thank you). What I am trying to do now is the same approach for a child grid of that same grid. I need to set SelectedDetailItem and SelectedDetailItems I believe the same approach will work but I cannot access my SelectedDetailItem  and SelectedDetailItems properties (which belong to my view model) from this child grid. I simply cannot get them to bind.

    I have tried a few different things such as neither of which have worked.

    SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.
    SelectedDetailItem}"

    and

    SelectedItem="{Binding SelectedDetailItem,ElementName=myViewName}"
    
     

    Do you have any ideas on how to bind the SelectedItem from a child grid to a property on a view model?
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 13 Jul 2012 Link to this post

    Hello,

     I would suggest you to expose your ViewModel (where the SelectedDetailItem is defined) as a StaticResource and set it as a Source for the Binding.

    For example:

    SelectedItem="{Binding SelectedDetailItem, Source={StaticResource ViewModel}}"


    All the best,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. UI for WPF is Visual Studio 2017 Ready
  4. David
    David avatar
    3 posts
    Member since:
    Jul 2012

    Posted 13 Jul 2012 Link to this post

    I am not able to access the viewmodel in the view. I am setting the DataContext of the view in the code behind to the viewModel.  
     I am using Prism and Dependency Injection and cannot seem to setup a resource for the ViewModel -maybe I am doing something wrong...? Or maybe this won't work for DI?
  5. David
    David avatar
    3 posts
    Member since:
    Jul 2012

    Posted 16 Jul 2012 Link to this post

    I resolved this by moving the "SelectedDetailItem" collection to my model. This allows for the collection to be bound to inside of the nested grid. This has solved my problem for now.
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 17 Jul 2012 Link to this post

    Hi,

     Thank you for sharing the solution to your problem with the community.

    Regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  7. papadi
    papadi avatar
    35 posts
    Member since:
    Nov 2008

    Posted 18 Sep 2012 Link to this post

    I think there is a flow in this implementation. I understand that we have something like this code:
    public class CompanyViewModel
    {
        public Company SelectedCompany { get; set; }
    }
     
    public class Company
    {
        public Employee SelectedEmployee { get; set; }
        public Employee[] Employees { get; set; }
    }
     
    public class Employee {}

    Consider the following data:
    -- Microsoft
    ----- Jim
    ----- John
    ----- Jack
    -- Telerik
    ----- Jane
    ----- Paul
    ----- Ralph

    1. User clicks 'Microsoft', so SelectedCompany is 'Microsoft'
    2. User clicks 'John', so SelectedEmployee is 'John'
    3. User clicks 'Paul'. However SelectedCompany is still Microsoft, and SelectedEmployee is still John.
Back to Top
UI for WPF is Visual Studio 2017 Ready