0


Im trying to achive something like here WPF DataGrid Grouping with sums and other fields to sum up 'quantity' property from items in a group. But it throws exception in foreach loop that it cant convert 'items' to my Type ('OrderItem' of which i thought items should be...)
C#

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value is ReadOnlyObservableCollection<Object>)
        {
            var items = ((ReadOnlyObservableCollection<Object>) value);
            Decimal total = 0;
            foreach (OrderItem gi in items )
            {
                total += gi.quantity;
            }
            return total.ToString();

        }
        return "0";
    }

XAML

<ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander>
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="{Binding Path=Name}" Margin="5"/>
                                                    <TextBlock Text="Count" Margin="5" />
                                                    <TextBlock Text="{Binding Path=Items, Converter={StaticResource additionConverter}}" Margin="5"/>
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>

and resource

<local:AdditionConverter x:Key="additionConverter" />

how to get access to the base element of 'value'? I puted a breakpoint and it is there. Sorry if i messed up something, i'm new to WPF.

Community
  • 1
  • 1
kasperro
  • 3
  • 2
  • What is the type of item which it is showing if not OrderItem ? – AnjumSKhan Sep 20 '16 at 08:47
  • Cannot implicitly convert type 'MS.Internal.Data.CollectionViewGroupInternal' to 'Plan_Produkcji_Beta.OrderItem' – kasperro Sep 20 '16 at 08:55
  • My guess is it would be something to do with your binding, in that i am thinking that your binding to a property of a WPF control rather than directly to the data. What can you provide the xaml for the item using the Converter? – Ben Steele Sep 20 '16 at 09:02
  • I have posted it in my question – kasperro Sep 20 '16 at 09:07
  • Is `Items` part of the OrderItem type? I suspect you want to bind to the collection of items, but you have the wrong DataContext in the TextBlock binding for `Items`. – Lennart Sep 20 '16 at 09:18
  • no its not. probably thats the point but i have no idea how to change it. All i want is to sum up a quantity property of items in group – kasperro Sep 20 '16 at 09:24

1 Answers1

0

Replace this :

   foreach (OrderItem gi in items )
        {
            total += gi.quantity;
        }

with :

   foreach (CollectionViewGroup group in items)
     {
        foreach(OrderItem item in group.Items)
        {
           total += item.quantity;
        } 
     }

and tell if this solves your problem.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38