如何在WPF列表框排序?如何在、列表、WPF

2023-09-05 23:54:45 作者:不了解我゛就别说我变了

在C#4.0 WPF应用程序,请参见code以下,显示了启动:

The C# 4.0 WPF application, see the code below, shows on startup:

ABD点击排序按钮与后btnSort_Click()单击事件处理程序:

abd after clicking the Sort button with btnSort_Click() click event handler:

如何排序,以便AAA,BBB,CCC?

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:

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>

更新: 好吧,我只是跟着文章排序一个WPF列表框项目

Update: Well, I simply followed the article "Sorting a WPF ListBox Items"

那么,什么是我感到属性内容和排序顺序中是财产的内容,我不知道(试图将其更改为任意的FFF,而不是内容已经得到了相同的,在第2个截图,结果?

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?

推荐答案

既然你排序字符串列表,并不表明属性名称(SortDescription的第一个参数):

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));
 
精彩推荐
图片推荐