在组合框中对项目分组 [英] Grouping items in a combobox
本文介绍了在组合框中对项目分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个listview包含两种类型的对象,单和多。
单个是一个普通的文本块,而多个是一个带有项目的组合框。
我试图在组合框中对项目进行分组,但没有成功。是否可以?
$ b
我想要达成的目标:
[Combobox v]
[Header]
[Item]
[Item]
[Header]
[Item]
解决方案
使用ListCollectionView与GroupDescription作为ItemsSource,并只提供一个GroupStyle到你的ComboBox。查看下面的示例:
XAML:
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 350Width =525>
< StackPanel>
< ComboBox x:Name =comboBox>
< ComboBox.GroupStyle>
< GroupStyle>
< GroupStyle.HeaderTemplate>
< DataTemplate>
< TextBlock Text ={Binding Name}/>
< / DataTemplate>
< / GroupStyle>
< /ComboBox.GroupStyle>
< ComboBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding Name}/>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>
< / StackPanel>
< / Window>
代码隐藏:
命名空间StackOverflow
{
///< summary>
/// 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;组; }
public string Category {get;组; }
}
}
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 trying to group the items in the combobox without success. Is it possible? Or should i go for a different approach?
What i'm trying to achieve:
[Combobox v]
[Header ]
[ Item]
[ Item]
[Header ]
[ Item]
解决方案
It is possible. Use a ListCollectionView with a GroupDescription as the ItemsSource and just provide a GroupStyle to your ComboBox. See sample below:
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>
Code-behind:
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; }
}
}
这篇关于在组合框中对项目分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文