在 WPF 中,如何在 DataGrid.RowDetailsTemplate 中绑定?
我有一个 DataGrid,它绑定到自定义类的 ObservableCollection。该集合是 ViewModel 中的一个属性,并且网格工作正常,包括对集合的更改。
我想使用 RowDetailsTemplate 在 StackPannel 中显示两个附加集合,如下所示:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Name="GrcidSP" Orientation="Horizontal">
<DataGrid Name="ClusterIndexGrid" AutoGenerateColumns="True"
ItemsSource="{Binding ClusterIndexColumns}"
CanUserAddRows="False" CanUserResizeRows="False"
Width="Auto"/>
<DataGrid Name="IndexGrid" AutoGenerateColumns="True"
ItemsSource="{Binding IndexColumns}"
CanUserAddRows="False" CanUserResizeRows="False"
Width="Auto"/>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
ClusterIndexColumns 和 IndexColumns,内部网格的绑定源,也是 ViewModel 中的集合。
问题是,当我显示行详细信息时,数据网格是空的,即根本没有加载,没有列或行。
为了了解发生了什么,我用 label: 替换了内部数据网格,
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Name="GridSP" Orientation="Horizontal">
<Label Content="{Binding}" Width="Auto" Background="AliceBlue"/>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
并且没有指定绑定源 - {Binding}
标签现在显示自定义类的名称,该类位于作为源的集合中外网格。
因此,不知何故,我需要导航回 ViewModel 作为内部网格的数据上下文。
但请告诉我怎么做?
I have a DataGrid which is bound to an ObservableCollection of a custom class. The collection is a property in a ViewModel, and the grid works fine, including with changes to the collection.
I want to use the RowDetailsTemplate to display two additional collections in a StackPannel, like so:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Name="GrcidSP" Orientation="Horizontal">
<DataGrid Name="ClusterIndexGrid" AutoGenerateColumns="True"
ItemsSource="{Binding ClusterIndexColumns}"
CanUserAddRows="False" CanUserResizeRows="False"
Width="Auto"/>
<DataGrid Name="IndexGrid" AutoGenerateColumns="True"
ItemsSource="{Binding IndexColumns}"
CanUserAddRows="False" CanUserResizeRows="False"
Width="Auto"/>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
ClusterIndexColumns and IndexColumns, the binding sources of the inner grids, are also collections in the ViewModel.
Problem is, when i display the rowdetails, the datagrids are empty, i.e. not loaded at all, no columns or rows.
In a bid to understand what is going on, i replaced the inner datagrids with label:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Name="GridSP" Orientation="Horizontal">
<Label Content="{Binding}" Width="Auto" Background="AliceBlue"/>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
and did not specify the binding source - {Binding}
The label now displays the name of the custom class which is in the collection that is the source of the outer grid.
So somehow, i need to navigate back to the ViewModel as the data context for the inner grids.
But how, pray tell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我通过将 ClusterIndexColumns 和 IndexColumns 集合作为属性添加到显示在主网格中的自定义类来解决这个问题。现在,当我单击该行时,行详细信息区域将打开,并且内部网格已正确加载。
顺便说一句,当主网格中选定的行发生更改时,通过将 SelectedIndex 属性绑定到 ViewModel 中的属性来填充集合,
希望这对某人有帮助。
OK, so I solved this by adding the ClusterIndexColumns and IndexColumns collections as properties to the custom class which is the displayed in the main grid. Now, when i click on the row, the rowdetails area is opened and the inner grids are loaded fine.
BTW, the collections are populated when the selected row changes in the main grid, by binding the SelectedIndex property to a property in the ViewModel, using
Hope this helps someone.