This is a migrated thread and some comments may be shown as answers.

Storing the Aggregate Function values from Grid to a ViewModel Property

2 Answers 168 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pratik
Top achievements
Rank 1
Pratik asked on 10 Sep 2013, 05:57 PM
Hi -

I have 2 user controls which are bound to the same ViewModel. See attached diagram. I am using the Aggregate Functions to get Sum,Count and average. 
UC2 - where the grid exists - doesnt need to show these aggregate values
UC1 - has textboxes which will be displaying the aggregate values. 

Since they both have the datacontext of the a same CommonViewModel - how can i set a property from UC2 of an aggregate value to a ViewModel Property - so then be leveraged in UC1. 

Obviously if I had to show the values in UC2 directly - then i can do XAMl based simple stuff like this - but this doesnt help me here - since I want to show these values in UC1 (and not UC2)
<StackPanel Grid.Row="1">
           <TextBlock Margin="10,0,0,0" Text="{Binding AggregateResults[\AppCount\].FormattedValue, ElementName=FIBRE_App_Stats}" />
           <TextBlock Margin="10,0,0,0" Text="{Binding AggregateResults[\GrandTotal\].FormattedValue, ElementName=FIBRE_App_Stats}" />
 </StackPanel>


Am attaching a complete working sample solution also to highlight the issue. Please see the CommonViewmodel and the 3 properties in there which i want to set with the aggregate value calculated from the gird in UC2.

Link for the working solution is as follows (since i cant upload it here) - https://github.com/pratikrshah/telerikaggsample


Can somebody please help

Thanks

2 Answers, 1 is accepted

Sort by
0
Pratik
Top achievements
Rank 1
answered on 12 Sep 2013, 01:46 AM
Can somebody please help
0
Dimitrina
Telerik team
answered on 12 Sep 2013, 02:56 PM
Hi,

First you need to have Sum, Count and Average properties defined in your ViewModel.

Then, you should implement your own logic for calculating them (for example this will be done every time when a collection changed event is raised from the bound source collection).

void source_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    //Update aggregate results.
    Sum = source.OfType<Player>().Select(c => c.Number).Sum();
    Average = source.OfType<Player>().Select(c => c.Number).Average();
}

After that you need to redefine the Footer for the GridViewDataColumn and show the aggregate values you have calculated . The "Totals" WPF Demo shows an example on how to redefine the Footer.
For example:
<telerik:GridViewDataColumn.Footer>
    <StackPanel TextElement.FontStyle="Italic">
        <StackPanel Orientation="Horizontal">
            <Label Content="Avg:" />
            <Label  Margin="5,0,0,0" Content="{Binding Path=Average, Source{StaticResource MyViewModel}"/>
        </StackPanel>
        ...
    </StackPanel>
</telerik:GridViewDataColumn.Footer>

I hope this helps.


Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Pratik
Top achievements
Rank 1
Answers by
Pratik
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or