简体   繁体   中英

WPF ListView.GroupStyle not displaying group header

I have the following xaml:

<ListView Name="lstCurrentAccounts">
    <ListView.View>
        <GridView x:Name="grvCurrentAccounts" AllowsColumnReorder="True">
            <GridViewColumn DisplayMemberBinding="{Binding AccountName}" Width="Auto" Header="Account Name" />
            <GridViewColumn DisplayMemberBinding="{Binding ReconciledBalance}" Width="Auto" Header="Reconciled" />
            <GridViewColumn DisplayMemberBinding="{Binding Balance}" Width="Auto" Header="Balance" />
        </GridView>
    </ListView.View>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

And the following code-behind:

private void updateData()
{
    MyContext dc = new MyContext();
    var c = (from x in dc.Accounts select x).ToList();
    lstCurrentAccounts.ItemsSource = c;

    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lstCurrentAccounts.ItemsSource);
    PropertyGroupDescription groupDescription = new PropertyGroupDescription("AccountTypeName");

    view.GroupDescriptions.Add(groupDescription);

    var r = (from x in dc.RepeatingTransactions where x.Deleted == false orderby x.NextOccurence select x).Take(10).ToList();
    lstRepeating.ItemsSource = r;

}

Now this should display a ListView with the data grouped by "AccountTypeName", which is a string property of each Account element. When I run the application the data is grouped properly, however the group Header, as defined in the GroupStyle.HeaderTemplate is blank, as opposed to displaying the appropriate AccountTypeName.

What am I doing wrong?

Ok so I have the solution and an explanation. I have two tables AccountType and Account, linked by a 1-many relationship. The field AccountTypeName exists in AccountType, however it is a non-scaffolded (using EF) read-only property in Account, and references the appropriate AccountType and pulls back the AccountTypeName from the AccountType table (still with me?) Now this field, AccountTypeName in Account seems not to get evaluated, so I have changed the line in code to:

PropertyGroupDescription groupDescription = new PropertyGroupDescription("AccountType");

In the Xaml I have changed the appropriate lines to:

<GroupStyle>
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
</GroupStyle>

This now requires me to override ToString() in AccountType table, which just returns the AccountTypeName field, which is now displayed as the group header.

I believe that the TextBlock should be bound to AccountTypeName instead of Name.

    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding AccountTypeName}" />
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>

I think you need to assign a CollectionViewSource to the ItemSource of lstCurrentAccounts instead of your original data. CollectionViewSource prepares the data for sorting and grouping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM