【问题标题】:How to sort in a WPF ListBox?如何在 WPF 列表框中排序?
【发布时间】:2013-03-14 01:35:13
【问题描述】:

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:

<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>

更新:
好吧,我只是跟着文章"Sorting a WPF ListBox Items"

那么,我按属性“内容”排序的顺序是什么,该属性“内容”在哪里,我想知道(试图将其更改为任意“fff”而不是“内容”得到相同,如第二张截图所示,结果?

【问题讨论】:

    标签: c# .net wpf sorting event-handling


    【解决方案1】:

    由于您要对字符串列表进行排序,因此不要指定属性名称(SortDescription 的第一个参数):

    listBox1.Items.SortDescriptions.Add(
                new System.ComponentModel.SortDescription("",
                System.ComponentModel.ListSortDirection.Ascending));
    

    【讨论】:

    • 我不确定您现在到底在问什么...将代码更改为我的建议是否解决了这个问题?
    • 它确实有效。我想了解SortDescription() 方法的第一个参数是什么,为什么在提供任意propertyName 后它没有给出错误+ 为什么顺序完全改变(以及根据哪些排序规则)?
    • 嗯,它确实给出了一个错误。如果您使用任意属性名(例如 fff)并在 Visual Studio 中打开您的 output window,您将看到绑定错误,例如:System.Windows.Data Error: 40 : BindingExpression path error: 'fff' property not found on 'object' ''String' (HashCode=536926232)'.
    • 谢谢。我没有注意到那个错误!但是如果它给出了一个错误,为什么项目的顺序会改变,我假设被重新排序?
    【解决方案2】:

    对 wpf 组合框或列表框进行排序很容易 - 但请记住包含 Imports System.ComponentModel

    要按字母顺序排序,只需

    MylistBox.Items.SortDescriptions.Add(
        New SortDescription("", ListSortDirection.Ascending))
    

    MyComboBox.Items.SortDescriptions.Add(
        New SortDescription("", ListSortDirection.Ascending))
    

    【讨论】:

      【解决方案3】:
      YOULISTBOX.Items.SortDescriptions.Clear(); 
      YOULISTBOX.Items.SortDescriptions.Add( new System.ComponentModel.SortDescription("NAME", System.ComponentModel.ListSortDirection.Ascending));
      

      确保每次更新

      【讨论】:

        【解决方案4】:

        附加信息:

        您排序的项目可以是任何DependencyProperty。因此,假设您有一个自定义类的ObservableCollection,它绑定到 ListBox 控件的ItemsSource。自定义类可以具有任意数量的依赖属性,您可以使用这些属性进行排序。您只需将依赖属性的名称(作为string)放入新的SortDescription 参数中。

        向控件添加多个SortDescriptions 将进行多变量排序。

        依赖属性可以表示任何类型的变量,而不仅仅是字符串。我有一个例子,我首先按bool 排序,然后按int,最后按DateTime

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-02-23
          • 1970-01-01
          • 2011-05-30
          • 1970-01-01
          • 1970-01-01
          • 2023-03-05
          • 2014-02-14
          • 1970-01-01
          相关资源
          最近更新 更多