我有一堂课Book
:
class Book
{
public int Id {get; set;}
public string Title {get; set;}
public string Authors {get; set;}
public string Genre {get; set;}
public virtual ICollection <Publication> Publications {get; set;}
}
表单上有两个(假设有A
和B
)DataGrid:A
显示Book
对象列表,B
显示所选书籍中的元素Publications
(即绑定到所选行中A
)。
问题是如何将 a 绑定B
到 ? 中的当前行A
?
我这样做了,但在我看来这并不完全正确:
<DataGrid x:Name="BooksGrid"
ItemsSource="{Binding Path=WorkingBooksSet, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="True" RowDetailsVisibilityMode="VisibleWhenSelected"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Id}" Width="Auto" />
<DataGridTextColumn Binding="{Binding Path=Title}" Width="*" />
<DataGridTextColumn Binding="{Binding Path=Authors}" Width="*" />
<DataGridTextColumn Binding="{Binding Path=Genre}" Width="*" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
// Second DataGrid
<DataGrid ItemsSource="{Binding ElementName=BooksGrid, Path=SelectedItem.Publications}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Publisher}" />
<DataGridTextColumn Binding="{Binding Path=ISBN}" />
...
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
WorkingBooksSet
在ObservableCollection
我的 ViewModel 中
所以我在这个装订备忘单中找到了解决办法。