WPF ListView按2列分组,但仅显示1个组标题
ListView显示以下类的集合:WPF ListView按2列分组,但仅显示1个组标题,wpf,listview,grouping,Wpf,Listview,Grouping,ListView显示以下类的集合: public class Employee { private string _department; private string _manager; private string _name; private string _address; public string Department { get { return _department; } } public str
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; }
}
}
部门和经理之间存在1对1的关系,因此具有相同部门的任意两行也将具有相同的经理
我想按部门/经理分组,组标题显示“部门(经理)”
我的CollectionViewSource看起来像
<CollectionViewSource x:Key="cvsEmployees" Source="{Binding Employees}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Department" />
<PropertyGroupDescription PropertyName="Manager" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
计划不显示第一级标题(部门),并从第二级标题以某种方式绑定到部门(第一级)和经理(第二级)
3个问题:
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
这看起来很笨重。有没有更优雅的方法跳过组标题感谢解决了主要障碍,上面的问题2:如何从组头绑定到非分组属性的属性 解决方案是将数据上下文更改为:
{Binding Items}
。ItemSource属性随后可用
<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>
我将创建另一个模型零件,它表示您需要进行的双重分组: 模型类:
public class EmployeeModel {
private readonly Employee _Employee;
public DepartmentManager ManagementInfo { get; private set; }
public string Name {
get { return _Employee.Name; }
}
public string Address {
get { return _Employee.Address; }
}
public EmployeeModel(Employee employee) {
this._Employee = employee;
this.ManagementInfo = new DepartmentManager(employee.Department, employee.Manager);
}
}
public class DepartmentManager {
public string Department { get; private set; }
public string Manager { get; private set; }
public DepartmentManager(string dept, string manager) {
this.Department = dept;
this.Manager= manager;
}
public override bool Equals(object obj) {
var model = obj as DepartmentManager;
if(null == model)
return false;
return Department.Equals(model.Department, StringComparison.InvariantCultureIgnoreCase) &&
Manager.Equals(model.Manager, StringComparison.InvariantCultureIgnoreCase);
}
}
XAML:
...
然后在窗口/视图模型中:
this.EmployeesModel = new ObservableCollection<EmployeeModel>(MyListOfEmployersFromDB.Select(e => new EmployeeModel(e)));
this.EmployeesModel=newobserveCollection(MyListOfEmployersFromDB.Select(e=>newemployeemodel(e));
注意,我在
DepartmentManager
类中重写了Equals
,但没有重写GetHashCode
,理想情况下,您应该对其进行自定义实现。我必须重写equals,以便分组视图源能够正确地分组相同的条目。您可以摆脱这种需求,为集合之外的相同员工购买部门经理
,并将其传递到员工模型
中心。您可以在列表视图中显示您的项目资源吗,构成您的ItemsSource.class的实际.cs类已添加到原始问题中,但仍希望回答问题1和3。谢谢
this.EmployeesModel = new ObservableCollection<EmployeeModel>(MyListOfEmployersFromDB.Select(e => new EmployeeModel(e)));