简体   繁体   English

WPF ListView.GroupStyle不显示组头

[英]WPF ListView.GroupStyle not displaying group header

I have the following xaml: 我有以下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. 现在,这应该显示一个ListView,其数据按“AccountTypeName”分组,这是每个Account元素的字符串属性。 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. 当我运行应用程序时,数据被正确分组,但是GroupStyle.HeaderTemplate中定义的组Header是空白的,而不是显示相应的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. 我有两个表AccountType和Account,由1-many关系链接。 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: AccountType字段存在于AccountType中,但它是Account中的非支架(使用EF)只读属性,并引用相应的AccountType并从AccountType表中提取AccountTypeName(仍然与我一起?)现在这个字段,AccountTypeName在帐户似乎没有得到评估,所以我已经将代码中的行更改为:

PropertyGroupDescription groupDescription = new PropertyGroupDescription("AccountType");

In the Xaml I have changed the appropriate lines to: 在Xaml中,我将相应的行更改为:

<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. 这现在要求我在AccountType表中覆盖ToString(),它只返回AccountTypeName字段,该字段现在显示为组头。

I believe that the TextBlock should be bound to AccountTypeName instead of Name. 我相信TextBlock应该绑定到AccountTypeName而不是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分配给lstCurrentAccounts的ItemSource而不是原始数据。 CollectionViewSource prepares the data for sorting and grouping. CollectionViewSource准备用于排序和分组的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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