2列,但只显示1组头WPF ListView的分组 [英] WPF ListView grouping by 2 columns but display only 1 group header

查看:190
本文介绍了2列,但只显示1组头WPF ListView的分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个ListView控件显示下面的类的集合:

<$ P为$ P> 公共类Employee
{
    私人字符串_department;
    私人字符串_manager;
    私人字符串_name;
    私人字符串_address;    公共字符串部
    {
        {返回_department; }
    }
    公共字符串经理
    {
        {返回_manager; }
    }
    公共字符串名称
    {
        {返回_name; }
    }
    公共字符串地址
    {
        {返回_address; }
    }
}

有是处和经理之间的1对1的关系,所以任何2排与同处也将有相同的经理。

我想通过组部/经理,与组头显示部门(经理)。

我的CollectionViewSource看起来像

 &LT; CollectionViewSource X:键=cvsEmployees来源={结合员工}&GT;
        &LT; CollectionViewSource.GroupDesc​​riptions&GT;
            &LT; PropertyGroupDesc​​ription属性名=部门/&GT;
            &LT; PropertyGroupDesc​​ription属性名=经理人/&GT;
        &LT; /CollectionViewSource.GroupDesc​​riptions>
    &LT; / CollectionViewSource&GT;

我们的计划是不显示第一级标题(部),并以某种方式结合到两个部门(第一级)和来自二级标题经理(2级)。

3个问题:


  1. 要避免显示第一级的头,我对groupstyle空数据模板:

     &LT; GroupStyle&GT;
        &LT; GroupStyle.HeaderTemplate&GT;
           &LT;&DataTemplate的GT;
           &LT; / DataTemplate中&GT;
        &LT; /GroupStyle.HeaderTemplate>
    &LT; / GroupStyle&GT;

    这似乎很笨重。是否有跳过组头更优雅的方式?


  2. 如何绑定从二级标题(经理)以达到所需的部(经理)第1组级属性(部门)?


  3. 有没有更好的方法,这样做不是创建2分组级别?


感谢


解决方案

解决的主要绊脚石,上述问题2:如何从组头绑定到不是分组属性的属性。

解决的办法是改变数据上下文: {绑定表项} 。该物业的ItemSource然后用

 &LT; GroupStyle&GT;
    &LT; GroupStyle.HeaderTemplate&GT;
        &LT;&DataTemplate的GT;
            &LT; StackPanel的方向=横向保证金=0,10,0,3的DataContext ={绑定表项}&GT;
                &LT; TextBlock的文本={绑定路径=部门}粗细=大胆保证金=3/&GT;
                &LT; TextBlock的文本={绑定路径=经理的StringFormat ='({0})}保证金=3/&GT;
            &LT; / StackPanel的&GT;
        &LT; / DataTemplate中&GT;
    &LT; /GroupStyle.HeaderTemplate>
&LT; / GroupStyle&GT;

A ListView displays a collection of the following class:

public class Employee
{
    private string _department;
    private string _manager;
    private string _name;
    private string _address;

    public string Department
    {
        get { return _department; }
    }
    public string Manager
    {
        get { return _manager; }
    }
    public string Name
    {
        get { return _name; }
    }
    public string Address
    {
        get { return _address; }
    }
}

There is a 1-to-1 relation between Department and Manager, so any 2 rows with the same department will also have the same manager.

I want to group by Department/Manager, with the group header showing "Department (Manager)".

My CollectionViewSource looks like

    <CollectionViewSource x:Key="cvsEmployees" Source="{Binding Employees}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Department" />
            <PropertyGroupDescription PropertyName="Manager" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

The plan is to not display the first level header (Department) and to somehow bind to both the Department (1st level) and the Manager (2nd level) from the 2nd level header.

3 questions:

  1. To avoid displaying the 1st level header, I have an empty data template in the groupstyle:

    <GroupStyle>
        <GroupStyle.HeaderTemplate>
           <DataTemplate>
           </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
    

    This seems very clunky. Is there a more elegant way to skip a group header?

  2. How do I bind to the 1st grouping level property (Department) from the 2nd level header (Manager) to achieve the required "Department (Manager)" ?

  3. Is there a better way to do this than creating 2 grouping level?

Thanks

解决方案

Solved the main stumbling block, question 2 above: how to bind from the group header to a property that is not the grouping property.

The solution is to change the data context to:{Binding Items}. The ItemSource properties are then available

<GroupStyle>
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,10,0,3" DataContext="{Binding Items}" >
                <TextBlock Text="{Binding Path=Department}" FontWeight="Bold" Margin="3"/>
                <TextBlock Text="{Binding Path=Manager, StringFormat='({0})'}" Margin="3"/>
            </StackPanel>
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
</GroupStyle>

这篇关于2列,但只显示1组头WPF ListView的分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆