【问题标题】:Bind a List's count to a Datagrid将列表的计数绑定到数据网格
【发布时间】:2012-03-05 09:09:18
【问题描述】:

我有以下数据类:

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&lt;CustomerProducts&gt;

我希望将CustomerOverview 集合绑定到DataGrid,以便您获得客户ID 和产品中所有ID 的计数。例如:

身份证........|产品数量

0001a | 3

bb032 | 0

3rt640 | 99

如何更改我的 XAML 来实现这一点,还是需要实现其他东西?

【问题讨论】:

标签: c# wpf data-binding observablecollection


【解决方案1】:

只需使用适当的绑定手动添加列:

<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>

【讨论】:

    【解决方案2】:

    您可以尝试向 DataGrid 添加列,例如:

    <DataGridTextColumn Binding="{Binding Products.Count}"></DataGridTextColumn>
    

    【讨论】:

      猜你喜欢
      • 2016-04-30
      • 2016-11-13
      • 2012-06-13
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 2011-01-31
      相关资源
      最近更新 更多