我有一个ListView,它包含两种类型的对象,single和multiple。single是一个普通的TextBlock,而multiple是一个包含项目的ComboBox。我正在尝试对组合框中的项目进行分组,但没有成功。这是否可行?或者我应该采用其他方法?我想达到的目标是:
[ComboBox v] [Header ] [ Item] [ Item] [Header ] [ Item]
im9ewurl1#
这是可能的。使用带有GroupDescription的ListCollectionView作为ItemsSource,只需向您的ComboBox提供GroupStyle。请参见下面的示例:XAML:
<Window x:Class="StackOverflow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:StackOverflow" xmlns:uc="clr-namespace:StackOverflow.UserControls" Title="MainWindow" Height="350" Width="525"> <StackPanel> <ComboBox x:Name="comboBox"> <ComboBox.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ComboBox.GroupStyle> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </StackPanel> </Window>
代码隐藏:
namespace StackOverflow { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //this.comboBox.DataContext = this; List<Item> items = new List<Item>(); items.Add(new Item() { Name = "Item1", Category = "A" }); items.Add(new Item() { Name = "Item2", Category = "A" }); items.Add(new Item() { Name = "Item3", Category = "A" }); items.Add(new Item() { Name = "Item4", Category = "B" }); items.Add(new Item() { Name = "Item5", Category = "B" }); ListCollectionView lcv = new ListCollectionView(items); lcv.GroupDescriptions.Add(new PropertyGroupDescription("Category")); this.comboBox.ItemsSource = lcv; } } public class Item { public string Name { get; set; } public string Category { get; set; } } }
d8tt03nd2#
这里是对ASanch answer的改进,使其对MVVM友好,其中您绑定到CollectionView。视图模型:
namespace StackOverflow { public class MainViewModel : INotifyPropertyChanged { public ObservableCollection<Item> Items { get; set; } public CollectionViewSource CollectionView { get; set; } public MainViewModel() { List<Item> items = new List<Item>(); items.Add(new Item() { Name = "Item1", Category = "A" }); items.Add(new Item() { Name = "Item2", Category = "A" }); items.Add(new Item() { Name = "Item3", Category = "A" }); items.Add(new Item() { Name = "Item4", Category = "B" }); items.Add(new Item() { Name = "Item5", Category = "B" }); Items = new ObservableCollection<Item>(items); var view = new CollectionViewSource(); view.GroupDescriptions.Add(new PropertyGroupDescription("Category")); view.Source = Items; CollectionView = view; } public CollectionViewSource CollectionView { get; set; } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } public class Item { public string Name { get; set; } public string Category { get; set; } } }
XAML:
<Window x:Class="StackOverflow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:StackOverflow" xmlns:uc="clr-namespace:StackOverflow.UserControls" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:MainViewModel/> </Window.DataContext> <StackPanel> <ComboBox ItemsSource="{Binding CollectionView.View}" DisplayMemberPath="Name"> <ComboBox.GroupStyle> <GroupStyle/> </ComboBox.GroupStyle> </ComboBox> </StackPanel> </Window>
2条答案
按热度按时间im9ewurl1#
这是可能的。使用带有GroupDescription的ListCollectionView作为ItemsSource,只需向您的ComboBox提供GroupStyle。请参见下面的示例:
XAML:
代码隐藏:
d8tt03nd2#
这里是对ASanch answer的改进,使其对MVVM友好,其中您绑定到CollectionView。
视图模型:
XAML: