我有以下数据类:
class CustomerProducts
{
public string Id { get; set; }
public List<ProductId> Products { get; set; }
}
以下XAML:
<DataGrid Name="grd_CustomerProducts" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}, Path=CustomerOverview}" />
注意:CustomerOverview
是主窗口中的ObservableCollection<CustomerProducts>
我希望将CustomerOverview
集合绑定到DataGrid,以便获取客户ID和产品中所有Id的计数。 e.g:
标识........ |产品数量
0001a | 3
bb032 | 0
3rt640 | 99
如何更改我的XAML以实现此目的,还是需要实现其他功能?
答案 0 :(得分:4)
只需使用正确的绑定手动添加列:
<DataGrid Name="grd_CustomerProducts"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MainWindow, AncestorLevel=1}, Path=CustomerOverview}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID"
Binding="{Binding Id}"
/>
<DataGridTextColumn Header="Number"
Binding="{Binding Path=Products.Count}"
/>
</DataGrid.Columns>
</DataGrid>
答案 1 :(得分:1)
您可以尝试将列添加到DataGrid,如:
<DataGridTextColumn Binding="{Binding Products.Count}"></DataGridTextColumn>