【问题标题】:Binding datagrid to sum将数据网格绑定到总和
【发布时间】:2018-01-17 07:13:29
【问题描述】:

在 mypage.xaml 中,我检索到了 ObservableCollection(Of MyObject)。 为了自动填充我的数据网格MyDataGrid 的(几乎所有)列,我像这样以编程方式放置了一个绑定

Class myPage
Private _context As New MyData

Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    _context.MySubObject.Load
    dataGrid.ItemsSource = _context.MySubObject.Local
End Sub
End Class

我像这样将MyDataGrid 的2 列绑定到MySubObject 的2 个属性

<DataGridTextColumn Header="ID" Binding="{Binding Id}"/>

<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>

这很好用。 现在,我想绑定第三列。我的问题是,MySubOject 的第三个属性是AnotherObject 的 ObservableCollection。我想为显示的每个MySubObject 显示AnotherObject 的对象数,例如:

ID | Name | NumberofAnotherObjects
----------------------------------
1  | abc  | 42
2  | def  | 56    
3  | xyz  | 592

我的想法是删除现有绑定并为所有三个创建一个 LINQ 查询。但我不知道如何在 LINQ 中动态构建这个总和。请给我一个提示好吗?

谢谢!

【问题讨论】:

  • 那么,是总和还是计数?如果计数:只需将列绑定到集合计数属性"{Binding AnotherObjectsCollection.Count}"
  • 这正是我想要的。谢谢

标签: wpf vb.net linq xaml datagrid


【解决方案1】:

集合具有Count 属性,可用于绑定

<DataGridTextColumn Header="NumberofAnotherObjects" 
                    Binding="{Binding AnotherObjectsCollection.Count}"/>

ObservableCollection 还会在 Count 更改(添加/删除项目)时提供通知

【讨论】:

    猜你喜欢
    • 2012-12-25
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2011-05-28
    • 1970-01-01
    • 2017-01-05
    相关资源
    最近更新 更多