Question

I have a grouped WPF DataGrid (the standard Microsoft one) representing some data on the UI for our users.

In order to show totals within the grouped regions, we are overriding the GroupItem DataTemplate as follows in XAML:

<Style TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Border BorderBrush="DarkGray" BorderThickness="1" Padding="12,0">
                    <Expander VerticalContentAlignment="Center" IsExpanded="{Binding ., Converter={Converters:ExpandedGroupConverter}}" ExpandDirection="Up">
                        <Expander.Header>
                                <Canvas>
                                **<TextBlock Text="{Binding} />**
                            </Canvas>
                        </Expander.Header>
                        <ItemsPresenter/>
                    </Expander>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

At runtime, currently, the TextBlock text binds to the DataContext, which is a CollectionViewGroup, which makes sense as the grid is binding to a CollectionView wrapping our datasource.

However, the CollectionViewGroup is very limited and does not give us access to its containing ViewModel, where we are storing properties such where to position groups (we're gathering coordinates from the columns when we first layout the grid), and need to bind to them, so that we can, for example, show a total directly above/below given column in a group.

In a nutshell we're trying to access more than just the CollectionView object from within a DataTemplate that targets a GroupItem. Any input on how to do this (or if there is a better approach to get summed totals per columns to show in group total templates) appreciated.

EDIT: So far, a workaround is to have a "Parent ViewModel" property on our items, although this bloats the model, I wish there was a more direct way to do this.

Was it helpful?

Solution

CollectionViewGroup gives you access to all items contained in this group. if you want to access other information from within your template, you can try binding with RelativSource.

EDIT:

so if you have a Collection of ItemVM, and on top of this a CollectionViewGroup on ItemVM.GroupProperty. then you can access your 1st ItemVM within a group with

 Binding={ Path = Items[0].AnyPropertyOnItemVM }

i think you will have to use a Converter if you want to calculate or do anything with the GroupItems

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top