Silverlight - 访问 DataGrid CellTemplate 的 DataTemplate 中的布局网格的 DataContext?
我正在使用 Silverlight 3 开发一个应用程序。在我的应用程序中,我有一个布局网格(名为“LayoutGrid”),其中有一个带有 DataGridTemplateColumns 的 DataGrid(名为“PART_datagrid”)。 LayoutGrid 设置了一个 DataContext,其中有一个梯形图列表作为属性。此梯形图列表设置为 PART_datagrid 的 ItemsSource。
<Grid x:Name="LayoutRoot">
<DataGrid x:Name="PART_datagrid" ItemsSource="{Binding Ladders}">
...
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />
现在,在 DataGridTemplateColumns 之一中,我有一个按钮,它应该调用 LayoutGrid 的 DataContext 中存在的命令。因此,我尝试在 DataTemplate 按钮上进行元素到元素绑定,如下所示
<Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />
但这似乎不起作用。我想要实现的是使用命令在父 DataContext 级别处理删除 DataGrid 行的事件。
有人可以建议我如何继续吗?
提前致谢...
I am using Silverlight 3 to develop an application. In my app, I have a layout Grid (named "LayoutGrid") in which I have a DataGrid (named "PART_datagrid") with DataGridTemplateColumns. The LayoutGrid is set a DataContext in which there is a Ladders list as a property. This Ladders list is set as the ItemsSource for the PART_datagrid.
<Grid x:Name="LayoutRoot">
<DataGrid x:Name="PART_datagrid" ItemsSource="{Binding Ladders}">
...
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />
Now in one of the DataGridTemplateColumns I have a button which should invoke a Command thats present in the LayoutGrid's DataContext. So I tried Element-To-Element binding on my DataTemplate button as follows
<Button Name="DeleteLadder" Click.Command="{Binding ElementName=LayoutRoot, Path=DataContext.DeleteLadderCommand}" />
But this does not seem to work. What I want to achieve is to handle the event of deletion of a DataGrid row at the parent DataContext level using the command.
Can someone pls suggest how do I proceed on this?
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎是每一行都使用数据网格源作为其“新”数据上下文之类的东西。因此,您需要从每一行中脱离网格并指向层次结构中更高的内容以获取父数据上下文。这些解决方案可能会有所帮助。当我遇到同样的问题时,解决方案 2 对我有用。
使用定位器的解决方案 1
请参阅此帖子:
解决
方案 2 使用顶部定义资源和连接到它的数据上下文。
然后在你的数据网格中使用类似的东西
祝你好运
The problem seems to be that each row uses the datagrid source as its "new" datacontext kind of thing. So from each row you need to get out of the grid and point to someting higher in the hierarchy to get the parent datacontext. These solutions may help. Solution 2 worked for me when I encountered the same problem.
Solution 1 using the Locator
See this posting:
Silverlight DataGrid.Celltemplate Binding to ViewModel
Solution 2 using define resource at the top and hooking up to its datacontext.
then use something like this inside your datagrid
Good Luck