如何在WPF列表框中排序? [英] How to sort in a WPF ListBox?
问题描述
C#4.0 WPF应用程序,请参阅下面的代码,在启动时显示:
abd点击排序按钮与 btnSort_Click()
点击事件处理程序:
如何按顺序排列aaa,bbb,ccc?
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:
< pre $
b xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =MainWindowHeight =350Width =525>
< Grid>
< ListBox Height =100HorizontalAlignment =LeftMargin =8,43,0,0Name =listBox1VerticalAlignment =TopWidth =120MouseDoubleClick =listBox1_MouseDoubleClick/> ;
< Button Content =SortHeight =23HorizontalAlignment =LeftMargin =140,94,0,0Name =btnSortVerticalAlignment =TopWidth =75Click = btnSort_Click/>
< / Grid>
< / Window>
更新:
嗯,我只是跟着文章排序WPF列表框项目
那么,我通过属性内容排序的顺序是什么,该属性Content在哪里,我想知道(试图改变它是任意的fff,而不是内容已经得到相同,如第二个截图,结果?
'重新排序一个字符串列表,不指示属性名称(SortDescription的第一个参数):
listBox1.Items。 SortDescriptions.Add(
new System.ComponentModel.SortDescription(,
System.ComponentModel.ListSortDirection.Ascending));
The C# 4.0 WPF application, see the code below, shows on startup:
abd after clicking the Sort button with btnSort_Click()
click event handler:
How can I sort in order aaa, bbb, ccc?
C# code:
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:
<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"
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?
Since you're sorting a list of strings, don't indicate a property name (first parameter of SortDescription):
listBox1.Items.SortDescriptions.Add(
new System.ComponentModel.SortDescription("",
System.ComponentModel.ListSortDirection.Ascending));
这篇关于如何在WPF列表框中排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!