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

Nested GridView DataContext

3 Answers 122 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ciaran O'Neill
Top achievements
Rank 1
Ciaran O'Neill asked on 10 May 2010, 07:26 AM
Hi,

I'm developing a Silverlight 4 app and am using the 2010 Q1 release 1 RadGridView.  I'm developing this app using the MVVM pattern and trying to keep my codebehind to a minimum.

On my View I have a RadGridView and this binds to a property on my ViewModel.  I am setting a property via the SelectedItem.  I have a nested RadGridView and I want to set a property on my ViewModel to the SelectedItem but I cannot.  I think the DataContext of my nested grid is the element in the parent's bound collection, rather than my ViewModel.  I can easily use codebehind to set my ViewModel property from the SelectionChanged event on the nested grid, but I'd rather not do this.  I have tried to use my viewModelName in the ElementName in my nested grid to specify that for SelectedItem, the ViewModel is the DataContext, but I cannot get this to work.  Any ideas?

Here is my Xaml:

<grid:RadGridView  
                    x:Name="master" 
                    ItemsSource="{Binding EntityClassList, Mode=TwoWay}" 
                    SelectedItem="{Binding SelectedEntityClass, Mode=TwoWay}" 
                    AutoGenerateColumns="False" 
                    > 
 
                <grid:RadGridView.Columns> 
                    <grid:GridViewSelectColumn></grid:GridViewSelectColumn> 
                    <grid:GridViewDataColumn DataMemberBinding="{Binding Description}" Header="Description"/. 
                </grid:RadGridView.Columns> 
 
                <grid:RadGridView.RowDetailsTemplate> 
                    <DataTemplate> 
                        <grid:RadGridView 
                            x:Name="child" 
                            ItemsSource="{Binding EntityDetails, Mode=TwoWay}" 
                            SelectedItem="{Binding DataContext.SelectedEntityDetail, ElementName='RequestView', Mode=TwoWay}" 
                            AutoGenerateColumns="False" 
                            > 
                                <grid:RadGridView.Columns> 
                                    <grid:GridViewSelectColumn></grid:GridViewSelectColumn> 
                                    <grid:GridViewDataColumn DataMemberBinding="{Binding ServiceItem}" Header="Service Item" /> 
                                    <grid:GridViewDataColumn DataMemberBinding="{Binding Comment}" Header="Comments" /> 
                                </grid:RadGridView.Columns> 
                        </grid:RadGridView> 
                    </DataTemplate> 
                </grid:RadGridView.RowDetailsTemplate> 
 
            </grid:RadGridView> 

3 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 12 May 2010, 11:46 AM
Hello Ciaran,

I'm sending you a sample project that illustrates how this can be implemented. Feel free to modify it for your own needs.

Regards,
Stefan Dobrev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Ciaran O'Neill
Top achievements
Rank 1
answered on 13 May 2010, 12:31 AM
Hi Stefan,

Thanks for taking the time to put together this solution for me.

Unfortunately I'm using DI in my application and my ViewModels do not have parameterless constructors as dependencies are getting injected and so cannot use a StaticResource as a solution to my problem.  My ViewModels are instantiating my Views and not the other way around.  I was hoping to be able to use Element Binding to get around this, but for the moment I have resorted to using codebehind.

Is there no option of using Element Binding on the Telerik Grid to solve this?

Thanks for your help,
Ciaran
0
Stefan Dobrev
Telerik team
answered on 13 May 2010, 08:24 AM
Hi Ciaran,

There is nothing that we can provide here, because this is a framework limitation when you have DataTemplate that get loaded dynamically. If you don't want to have code-behind you can create a simple attached behavior that does the trick.

Best wishes,
Stefan Dobrev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
Ciaran O'Neill
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Ciaran O'Neill
Top achievements
Rank 1
Share this question
or