【发布时间】:2014-02-17 17:46:55
【问题描述】:
我正在尝试根据我的以下模型对我的收藏进行分组:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Role PersonRole { get; set; }
}
public class Role
{
public int Id { get; set; }
public string RoleName { get; set; }
}
我的个人收藏,
public ObservableCollection<Person> PersonList;
集合中填充了三个 Person 对象,其中两个具有相同的角色。
我想根据他们的 RoleName 对我所有的人进行分组。我有以下 XAML:
<Grid.Resources>
<CollectionViewSource x:Key="cvs" Source="{Binding PersonList}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="PersonRole.RoleName"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource cvs}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding PersonRole.RoleName}" FontWeight="Bold" Background="ForestGreen" Margin="0,5,0,0"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
但是,呈现的 UI 未在列表框中显示分组的 RoleName。此外,我想水平显示分组,其中组水平显示,而分组项目垂直显示。提前感谢任何帮助。
这是我看到的输出:
【问题讨论】:
-
你想要很多...... ;) 把你的例子发给我们,这样我们就可以工作了。
-
好吧,实际上我只是想弄清楚为什么没有显示 RoleName。水平分组虽然是次要问题。
-
只需发布代码。我不想按照所有模板和测试数据的方式工作。你已经有了。
标签: wpf listbox itemscontrol