[英]ListBox with possibility to group by category and A-Z
I have two list of Parameters, where you can move them between the two (include or not) and I want to customize the order of the Parameters to simplify the selection. 我有两个参数列表,您可以在两个列表之间移动它们(包括或不包括),我想自定义参数的顺序以简化选择。
I am currently using a ListBox
with multiple selection, but I want it to have a group by feature to group by category
, AZ
or custom sort like a propertyGrid
. 我目前正在使用具有多个选择的
ListBox
,但是我希望它具有一个按功能分组,以便按category
, AZ
或自定义排序(如propertyGrid
进行分组。
Is there a component that can do all of those? 是否有可以完成所有这些任务的组件? What settings do I need to set on that component?
我需要在该组件上设置哪些设置?
I would use a ListView
which supports groups like it does in the Windows Explorer views. 我将使用像Windows资源管理器视图中那样支持组的
ListView
。
check this one: How to: Group Items in a Windows Forms ListView Control 检查这一点: 如何:将Windows窗体ListView控件中的项目分组
so in short, you first create a group: 简而言之,您首先创建一个组:
// Adds a new group that has a left-aligned header
listView1.Groups.Add(new ListViewGroup("List item text",
HorizontalAlignment.Left));
then you add items to a group for example here the item 0 is added to the group: 然后将项目添加到组中,例如,此处将项目0添加到组中:
// Adds the first item to the first group
listView1.Items[0].Group = listView1.Groups[0];
If you decide to use a ListView - here's a solution to use column-based sorting. 如果您决定使用ListView-这是使用基于列的排序的解决方案。 I use it in a component, inheriting ListView.
我在继承ListView的组件中使用它。
private ListViewItemComparer LviComparer = new ListViewItemComparer(); // Comparer Class
// Override OnColumn Click
protected override void OnColumnClick(ColumnClickEventArgs e)
{
if (e.Column == LviComparer.SortColumn)
{
if (LviComparer.Order == SortOrder.Ascending)
{
LviComparer.Order = SortOrder.Descending;
}
else
{
LviComparer.Order = SortOrder.Ascending;
}
}
else
{
LviComparer.SortColumn = e.Column;
LviComparer.Order = SortOrder.Ascending;
}
this.Sort();
base.OnColumnClick(e);
}
// Compare Class //比较类
public class ListViewItemComparer : IComparer {
public int SortColumn { get ; private set ; }
public SortOrder Order { get ; private set ; }
private CaseInsensitiveComparer objectCompare = new CaseInsensitiveComparer();
public int Compare(object x, object y)
{
int compResult = 0;
ListViewItem lviX, lviY;
lviX = (ListViewItem)x;
lviY = (ListViewItem)y;
compResult = CompareString(lviX, lviY);
if (Order == SortOrder.Ascending)
{
return compResult;
}
else if (Order == SortOrder.Descending)
{
return (-compResult);
}
else
{
return 0;
}
}
private int CompareString(ListViewItem lviX, ListViewItem lviY)
{
try
{
int compareResult = objectCompare.Compare(lviX.SubItems[SortColumn].Text, lviY.SubItems[SortColumn].Text);
return compareResult;
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
return 0;
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.