how can I add a Converter class to an Aggregate function

14 posts, 0 answers
  1. Brian Graves
    Brian Graves avatar
    48 posts
    Member since:
    May 2010

    Posted 21 May 2010 Link to this post

    My aggregate function returns a decimal number representing the number of minutes.  However, I want to use a Converter data binding class on this result to make it into a D H M (days, hours, minutes) format.  My converter class works fine on my column data but, unfortunately, I have no way of applying it to my column aggregate function.  How can I do this (apart from calling the convert method directly from my aggregate method)?
  2. Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 26 May 2010 Link to this post

    Hi Brian Graves,

    My suggestion is to use an aggregate function that returns a TimeSpan. Then you can use the ResultFormatString to format the time span to match your requirements.

    Greetings,
    Stefan Dobrev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Brian Graves
    Brian Graves avatar
    48 posts
    Member since:
    May 2010

    Posted 26 May 2010 Link to this post

    Thanks but I can't get my ResultFormatString to output my timespan correctly.  For example, I want my output to look like:
    19 D 8 H 14 M

    If I set my ResultFormatString to "{0:dd} D {0:hh} H {0:mm} M", I get the following result:
    19.08.14:00 D 19.08.14:00 H 19.08.14:00 M

    How can I output this correctly?
  5. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 31 May 2010 Link to this post

    Hello Brian Graves,
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Minutes}" >
        <telerik:GridViewDataColumn.Footer>
            <telerik:AggregateResultsList ItemsSource="{Binding}">
                <telerik:AggregateResultsList.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Converter={StaticResource MyConverter}}" />
                        </StackPanel>
                    </DataTemplate>
                </telerik:AggregateResultsList.ItemTemplate>
            </telerik:AggregateResultsList>
        </telerik:GridViewDataColumn.Footer>
    </telerik:GridViewDataColumn>

    The XAML above demonstrates a way to use a converter with the aggregate results.

    Regards,
    Pavel Pavlov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Siew Fai Hoy
    Siew Fai Hoy avatar
    12 posts
    Member since:
    Sep 2009

    Posted 15 Jul 2010 Link to this post

    Hi,

    This does the job in formatting the aggregate result value in the footer, what if I would like to achieve the same thing in the group header panel  (i.e. using a converter to format the value of aggregate result) ?

    Regards,
    Siew Fai Hoy
  7. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 21 Jul 2010 Link to this post

    Hi Siew Fai Hoy,

    You may use exactly the same approach , but you can use a global style targeting aggregate results list.
    This way it will affect all aggregate results list , including those in the headers.

    Regards,
    Pavel Pavlov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. MiddleTommy
    MiddleTommy avatar
    163 posts
    Member since:
    Nov 2009

    Posted 23 Jan 2012 Link to this post

    Could you go into more detail as to how this would be done? If I target global AggregateResultsList then wouldnt it use the converter for all columns and not just the one I need special formatting?
  9. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 23 Jan 2012 Link to this post

    Hello Tommy ,

    Indeed in case you need it applied to specific elements only , the global style will not work . You will need to take care individually with explicit styles.

    Kind regards,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  10. MiddleTommy
    MiddleTommy avatar
    163 posts
    Member since:
    Nov 2009

    Posted 23 Jan 2012 Link to this post

    Where would I apply the style to? In the column Definition somewhere? I have my footer formatted correctly with a Binding and Converter. but the Aggregate value in the group header is not correct.
    <Style x:Key="LengthAggregateStyle" TargetType="t:AggregateResultsList">
                    <Setter Property="ItemTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Value, Converter={StaticResource LengthConverter}}" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

    <t:GridViewDataColumn DataMemberBinding="{Binding length, Converter={StaticResource LengthConverter}}"
                                          IsReadOnly="True"
                                          Header="length">
                        <t:GridViewDataColumn.AggregateFunctions>
                            <t:SumFunction SourceField="TotalLength" FunctionName="TotalLengthSum"/>
                        </t:GridViewDataColumn.AggregateFunctions>
                         
                         
                        <t:GridViewDataColumn.Footer>
                            <t:AggregateResultsList ItemsSource="{Binding}" Style="{StaticResource LengthAggregateStyle}"/>
                        </t:GridViewDataColumn.Footer>
                        <t:GridViewDataColumn.GroupHeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Value, Converter={StaticResource LengthConverter}}" />
                            </DataTemplate>
                        </t:GridViewDataColumn.GroupHeaderTemplate>
     
                    </t:GridViewDataColumn>
  11. MiddleTommy
    MiddleTommy avatar
    163 posts
    Member since:
    Nov 2009

    Posted 23 Jan 2012 Link to this post

    See my screen shot just so I know we are on the same page
  12. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 24 Jan 2012 Link to this post

    Hi,

     Have you tried implicit style? Please remove the x:Key setting from your style definition. 

    Regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  13. MiddleTommy
    MiddleTommy avatar
    163 posts
    Member since:
    Nov 2009

    Posted 24 Jan 2012 Link to this post

    Implicit styling does not work. It styles all the footer cells (some incorrectly) and none of the group Headers. See the attached picture.
  14. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 30 Jan 2012 Link to this post

    Hello Tommy ,

    Indeed applying the template to the aggregate results in the group header is a bit tricky.
    I believe for your specific case there is a lighter solution without styling and retemplating.
    We can place the converting /formatting logic in the aggregate function itself as demonstrated in the sample project attached. 

    Here we have both the group aggregates and the common aggregates displaying the right formatting.

    All the best,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  15. MiddleTommy
    MiddleTommy avatar
    163 posts
    Member since:
    Nov 2009

    Posted 30 Jan 2012 Link to this post

    Thanks Pavel. That example worked great. I even turned it into an AttachedProperty so I can add it to columns easily and still follow MVVM.
Back to Top
DevCraft banner