views:

653

answers:

2

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}" 
                          /* Note: SelectedItem here is not setting my property in my ViewModel*/
                            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> 
A: 

Looks like you are on the right track, if your "SelectedEntityDetail" prop on your VM is at the same level as "SelectedEntityClass"

Try binding at the same level as you "master" grid or soemthing above in the tree "LayoutRoot" etc.. as I only guess what requestView is.

SelectedItem="{Binding ElementName=master, Path=DataContext.SelectedEntityDetail}"

76mel
Yes, "SelectedEntityDetail" and "SelectedEntityClass" are at the same level in my VM. "requestView" is the xName of my View, so what I was trying to do was set the ElementName to the root of the View so I have access to the original View DataContext. This kind of syntax works in WPF as far as I know but I can't seem to get it in Silverlight. Binding to master as you have suggested also doesn't work.
Ciaran
A: 

Have been in contact with Telerik support and they tell me that this is a framework limitation. They have suggested a workaround using an attached behaviour. So far I've stuck with the small piece of codebehind

Ciaran