C# 如何在 WPF 列表框中排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15600099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort in a WPF ListBox?
提问by Fulproof
The C# 4.0 WPF application, see the code below, shows on startup:
C# 4.0 WPF 应用程序,见下面的代码,在启动时显示:
abd after clicking the Sortbutton with btnSort_Click()
click event handler:
单击带有单击事件处理程序的排序按钮后的 abd btnSort_Click()
:
How can I sort in order aaa, bbb, ccc?
如何按 aaa、bbb、ccc 的顺序排序?
C# code:
C#代码:
public MainWindow()
{
InitializeComponent();
listBox1.Items.Add("ccc");
listBox1.Items.Add("aaa");
listBox1.Items.Add("bbb");
}
private void btnSort_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.SortDescriptions.Add(
new System.ComponentModel.SortDescription("Content",
System.ComponentModel.ListSortDirection.Ascending));
}
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
listBox1.Items.RemoveAt
(listBox1.Items.IndexOf(listBox1.SelectedItem));
}
XAML:
XAML:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Height="100" HorizontalAlignment="Left" Margin="8,43,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" MouseDoubleClick="listBox1_MouseDoubleClick" />
<Button Content="Sort" Height="23" HorizontalAlignment="Left" Margin="140,94,0,0" Name="btnSort" VerticalAlignment="Top" Width="75" Click="btnSort_Click" />
</Grid>
</Window>
Update:
Well, I simply followed the article "Sorting a WPF ListBox Items"
更新:
嗯,我只是按照文章“排序 WPF 列表框项目”
So, what is the order by which I am sorting by a property "Content" and where is that property "Content", I wonder (tried to change it to arbitrary "fff" instead of "Content" having gotten the same, as in 2nd screenshot, results?
那么,我按属性“内容”排序的顺序是什么,该属性“内容”在哪里,我想知道(试图将其更改为任意的“fff”而不是“内容”已经变得相同,如第二张截图,结果?
采纳答案by Blachshma
Since you're sorting a list of strings, don't indicate a property name (first parameter of SortDescription):
由于您正在对字符串列表进行排序,因此不要指明属性名称(SortDescription 的第一个参数):
listBox1.Items.SortDescriptions.Add(
new System.ComponentModel.SortDescription("",
System.ComponentModel.ListSortDirection.Ascending));
回答by John
It's easy to sort a wpf combobox or listbox - but remember to include Imports System.ComponentModel
.
对 wpf 组合框或列表框进行排序很容易 - 但请记住包括 Imports System.ComponentModel
。
To sort in alphabetical order, simply
要按字母顺序排序,只需
MylistBox.Items.SortDescriptions.Add(
New SortDescription("", ListSortDirection.Ascending))
or
或者
MyComboBox.Items.SortDescriptions.Add(
New SortDescription("", ListSortDirection.Ascending))
回答by Jeba Ra
YOULISTBOX.Items.SortDescriptions.Clear();
YOULISTBOX.Items.SortDescriptions.Add( new System.ComponentModel.SortDescription("NAME", System.ComponentModel.ListSortDirection.Ascending));
to ensure it update every time
以确保它每次更新
回答by Tim
Additional info:
附加信息:
The item that you sort with may be any DependencyProperty
. So lets say that you have an ObservableCollection
of a custom class that is bound to the ItemsSource
of the ListBox control. The custom class can have any number of dependency properties, and you can use those for the sort(s). You just put the name of the dependency property (as a string
) in the new SortDescription
argument.
您排序的项目可能是 any DependencyProperty
。因此,假设您有一个ObservableCollection
绑定到ItemsSource
ListBox 控件的自定义类。自定义类可以具有任意数量的依赖属性,您可以将它们用于排序。您只需将依赖属性的名称(作为 a string
)放在新SortDescription
参数中。
Adding multiple SortDescription
s to the control will do a multi-variable sort.
向SortDescription
控件添加多个s 将进行多变量排序。
The dependency properties may represent any type of variable and not just strings. I have an example where I am sorting first by a bool
, then by an int
, and finally by DateTime
.
依赖属性可以表示任何类型的变量,而不仅仅是字符串。我有一个示例,我首先按 a 排序bool
,然后按 an排序int
,最后按DateTime
.