bluefox
bluefox

Reputation: 175

Binding datagrid to sum

In mypage.xaml I have retrieved an ObservableCollection(Of MyObject). To fill (almost all) columns of my datagrid MyDataGrid automatically I placed a binding programatically like this

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

and I bound 2 columns of MyDataGrid to 2 properties of MySubObject like this

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

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

This works fine. Now, I want to bind a 3rd column. My issue is, the 3rd property of MySubOject is an ObservableCollection of AnotherObject. I want to display the number of objects of AnotherObject for each MySubObject that is displayed, like:

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

My idea would be to remove the existing bindings and create a LINQ query for all three. But I don't know how to dynamically build this sum in LINQ. Could you please give me a hint?

Thanks!

Upvotes: 0

Views: 100

Answers (1)

ASh
ASh

Reputation: 35646

collections have Count property which can be used in binding

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

ObservableCollection additionally provides notification when Count changes (items added/removed)

Upvotes: 1

Related Questions