One approach is to use MultiBinding to search or calculate user data and bind time.
I made a DataGrid with groups that show in the header the sum of the specific values in the group in order to update this amount when the elements of the group change. I made a multi-valued binding with a custom multi-valued converter, a multi-valued binding with the ItemCount property allows you to receive notifications when group items change, and then update the amount and display the value of the news feed.
Here is the code for the multi-valued converter class:
Public Class UserBalanceConverter Implements IMultiValueConverter Private Function GetSubTotal(ByVal obj As CollectionViewGroup) As String Dim total As Decimal For Each objItem As Object In obj.Items If TypeOf objItem Is Account Then Dim a As Account = DirectCast(objItem, Account) Dim rate As Decimal = 1 rate = 1 / ExchangeRatesInfo.GetExchangeRate(a.currencyCode.ToString) total += a.Balance * rate Else total += GetSubTotal(objItem) End If Next Return total.ToString("C") End Function Public Function Convert(ByVal value() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) _ As Object Implements System.Windows.Data.IMultiValueConverter.Convert Dim cvg As CollectionViewGroup = CType(value(1), CollectionViewGroup) Return GetSubTotal(cvg) End Function Public Function ConvertBack(ByVal value As Object, ByVal targetType() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) _ As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack Throw New NotImplementedException End Function End Class
Then in XAML you use a multi-valued converter in the style used for GroupItem:
<Style TargetType ="{x:Type GroupItem}" x:Key="UserGroupHeaderStyle"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <Expander x:Name="exp" IsExpanded="False"> <Expander.Header> <StackPanel > <TextBlock Text="{Binding Name}" /> <StackPanel Orientation="Horizontal" > <TextBlock Text="{Binding ItemCount}"> <TextBlock Text=" "/> <TextBlock Text="items" /> <TextBlock Text=" "/> <TextBlock Text="Balance: " /> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource UserBalanceConverter}"> <Binding Path="ItemCount"/> <Binding /> </MultiBinding> </TextBlock.Text> </TextBlock> </StackPanel> </StackPanel> </Expander.Header> <ItemsPresenter /> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style>
Finish applying the style to the DataGrid:
<DataGrid.GroupStyle> <GroupStyle ContainerStyle="{StaticResource UserGroupHeaderStyle}"> <GroupStyle.Panel> <ItemsPanelTemplate> <DataGridRowsPresenter/> </ItemsPanelTemplate> </GroupStyle.Panel> </GroupStyle> </DataGrid.GroupStyle>
Also, be sure to declare your conversion class in the resource section of your XAML:
<ResourceDictionary> <ResourceDictionary.MergedDictionaries> <local:UserBalanceConverter x:Key="UserBalanceConverter"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
Et Voilà! It works like a charm!
NTN