Silverlight嵌套RadGridView SelectedItem DataContext
我正在开发一个 Silverlight 4 应用程序,并使用 2010 年第一季度发布 1 RadGridView。我正在使用 MVVM 模式开发这个应用程序,并尝试将代码隐藏保持在最低限度。
在我的视图上,我有一个 RadGridView,它绑定到我的 ViewModel 上的一个属性。我正在通过 SelectedItem
设置属性。我有一个嵌套的 RadGridView,我想将 ViewModel 上的属性设置为 SelectedItem
但我不能。我认为嵌套网格的 DataContext 是父级绑定集合中的元素,而不是我的 ViewModel。我可以轻松地使用代码隐藏从嵌套网格上的 SelectionChanged
事件中设置我的 ViewModel 属性,但我不想这样做。我尝试在嵌套网格的 ElementName 中使用 viewModelName 来指定对于 SelectedItem
,ViewModel 是 DataContext,但我无法使其正常工作。有什么想法吗?
这是我的 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你走在正确的轨道上,如果你的虚拟机上的“SelectedEntityDetail”道具与“SelectedEntityClass”处于同一级别
尝试绑定在与“master”网格或树“LayoutRoot”上面的东西相同的级别等。因为我只是猜测 requestView 是什么。
SelectedItem="{Binding ElementName=master, Path=DataContext.SelectedEntityDetail}"
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}"
已与 Telerik 支持联系,他们告诉我这是一个框架限制。他们提出了使用附加行为的解决方法。到目前为止,我一直坚持一小段代码隐藏
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