This is a migrated thread and some comments may be shown as answers.
Child RadGrid set SelectedItem on ViewModel MVVM
5 Answers 62 Views
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 12 Jul 2012, 08:40 PM
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?

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 13 Jul 2012, 05:38 AM
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 >>

0
David
Top achievements
Rank 1
answered on 13 Jul 2012, 03:24 PM
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?
0
David
Top achievements
Rank 1
answered on 16 Jul 2012, 03:58 PM
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.
0
Dimitrina
Telerik team
answered on 17 Jul 2012, 05:20 AM
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.

0
papadi
Top achievements
Rank 1
answered on 18 Sep 2012, 02:28 PM
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.
Asked by
David
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
David
Top achievements
Rank 1
papadi
Top achievements
Rank 1
Share this question
or