答案 0 :(得分:0)
试试这个,使用ListBox轻松将ListBox的ItemTemplate更改为Expander。 例如: ScreenShot XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowVM/>
</Window.DataContext>
<Grid>
<ListView ItemsSource="{Binding ItemsList}">
<ListView.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Name}" >
<ListView ItemsSource="{Binding Items}"/>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
视图模型:
public class MainWindowVM
{
public MainWindowVM()
{
ItemsList = new List<Group>();
var items = new List<Item>();
items.Add(new Item("Item1"));
items.Add(new Item("Item2"));
items.Add(new Item("Item3"));
//items.Add("Item4");
//items.Add("Item5");
ItemsList.Add(new Group()
{
Name = "List1",
Items = items
});
items.Add(new Item("Item4"));
ItemsList.Add(new Group()
{
Name = "List2",
Items = items
});
items.Add(new Item("Item5"));
ItemsList.Add(new Group()
{
Name = "List3",
Items = items
});
}
public List<Group> ItemsList { get; set; }
}
public class Group
{
public string Name { get; set; }
public List<Item> Items { get; set; }
public override string ToString()
{
return Name;
}
}
public class Item
{
public Item(string name)
{
Name = name;
}
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
答案 1 :(得分:0)
只要您使用WPF框架,就可以定义ListBox.GroupStyle以及内部项的常规样式。然后将列表框绑定到CollectionViewSource:
ICollectionView view = CollectionViewSource.GetDefaultView(*your-collection-here*);
view.GroupDescriptions.Add(new PropertyGroupDescription(*your-grouping-field-here*));
view.SortDescriptions.Add(new SortDescription((*your-grouping-field-here*, ListSortDirection.Ascending));
your-listbox-here.ItemsSource = view;
所有学分都在这里:http://www.c-sharpcorner.com/uploadfile/dpatra/grouping-and-sorting-in-listbox-in-wpf
要扩展/折叠组,您可以添加保持当前状态(折叠或展开)的bool属性,并在样式中考虑这一点(比如说,仅对展开的项目显示,更改&#34;展开/ collapse&#34; chevron icon等。)