Grouping items in a ComboBox

前端 未结 1 1186
醉梦人生
醉梦人生 2020-11-28 08:27

I have a ListView that contains two types of objects, single and multiple. The single is a ordinary TextBlock while the multiple is a ComboBox with items.

I\'m tryin

1条回答
  •  悲&欢浪女
    2020-11-28 08:57

    It is possible. Use a ListCollectionView with a GroupDescription as the ItemsSource and just provide a GroupStyle to your ComboBox. See sample below:

    XAML:

    
        
            
                
                    
                        
                            
                                
                            
                        
                    
                
                
                    
                        
                    
                
            
        
    
    

    Code-behind:

    namespace StackOverflow
    {
        /// 
        /// Interaction logic for MainWindow.xaml
        /// 
        public partial class MainWindow : Window
        {
    
            public MainWindow()
            {
                InitializeComponent();
                //this.comboBox.DataContext = this;
    
                List items = new List();
                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; }
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题