自排序列表框

发布于 2024-09-24 17:38:13 字数 533 浏览 6 评论 0原文

完全被一些看似简单的事情难住了,而且已经被搞死了……但仍然难住了。

我想做的:我有一个 WinForms ListBox。它的项目填充了对象,并且设置了 DisplayMember。当应用程序运行时,列出的项目中的数据可能会发生变化,包括 DisplayMember 后面的字段。我希望列表框中显示的文本在发生这种情况时发生变化,并且我还希望列表框重新排序,以便项目保持按字母顺序排列。

BindingList 可以很好地在数据更改时更新显示的文本,但对于我来说,我无法对其进行排序。

我回顾了这一点:http://msdn.microsoft.com/en-us/library /ms993236.aspx

这里还有很多关于如何执行此操作的线程,但似乎都不适用于列表框。

在 ListBox 上设置 Sorted 属性同样没有帮助。

我需要做什么才能让 ListBox 自行排序?

Totally stumped by something that seems easy, and has been done to death... Yet still stumped.

What I want to do: I've got a WinForms ListBox. Its items are populated with objects, the DisplayMember is set. As the app runs, the data in the listed items might change, including the field behind the DisplayMember. I want the text displayed in the ListBox to change when this happens, and I also want the ListBox to re-sort itself so the items remain in alphabetical order.

A BindingList works fine to update the displayed text when the data changes, but for the life of me, I can't get it to sort.

I reviewed this: http://msdn.microsoft.com/en-us/library/ms993236.aspx

Plus numerous threads here about how to do this, but none of it seems to work for a ListBox.

Setting the Sorted property on the ListBox is similarly unhelpful.

What do I need to do to get a ListBox to sort itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

半岛未凉 2024-10-01 17:38:13

您可以使用 BindingSource 对象。
只需将其拖放到表单中并将 ListBox.DataSource 属性指向此 BindingSource 对象即可。
然后转到 BindingSource 的属性并根据需要定义排序。

然后,您可以在代码中设置 myBindingSource.DataSource = myCollection ,瞧,您的列表框已填充并排序。
简单的。

You can use a BindingSource object.
Just drag-n-drop it into your form and point your ListBox.DataSource property to this BindingSource object.
Then go to the BindingSource's properties and define Sort as you need.

Then in code you can set myBindingSource.DataSource = myCollection and voila, your listbox is populated and sorted.
Easy.

酷遇一生 2024-10-01 17:38:13

与 Patrol02 的帖子一样,但是您可能需要尝试将 DataSource 设置为 null,然后根据列表大小更改触发的事件重新分配它。您可以在集合上使用观察者模式,重写 Add 和 Remove 方法来通知观察者重新绑定自身。

As with Patrol02's post, however you may want to try setting the DataSource to null and then reassigning it based on an event triggered by the list size changing. You could use the observer pattern on the collection, overriding the Add and Remove methods to notify watchers to rebind themselves.

宫墨修音 2024-10-01 17:38:13

重置数据源将有效地对列表框进行排序:

    listBox1.DataSource = null;
    listBox1.DataSource = myBindingList;
    listBox1.DisplayMember = "MyField";

但这不是自动的。据我了解,只要通过事件或类似的事件更新 DisplayMember 后面的字段,就应该进行排序......

无论如何,请参阅我的完整测试:

public partial class Form1 : Form
{
    public BindingList<ABC> myBindingList = new BindingList<ABC>();

    public Form1() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
        myBindingList.Add(new ABC("zzz"));
        myBindingList.Add(new ABC("aaa"));
    }

    private void button2_Click(object sender, EventArgs e) {
        myBindingList[0].MyField = "ccc"; // was "zzz"
        myBindingList[1].MyField = "ddd"; // was "aaa"

        listBox1.DataSource = null;
        listBox1.DataSource = myBindingList;
        listBox1.DisplayMember = "MyField";
    }

    private void Form1_Load(object sender, EventArgs e) {
        listBox1.DataSource = myBindingList;
        listBox1.DisplayMember = "MyField";

    }
}

public class ABC  {
    public string MyField { get; set; } 
    public ABC(string val) {
        MyField = val;
    }
}

Resetting the DataSource will effectively sort the ListBox:

    listBox1.DataSource = null;
    listBox1.DataSource = myBindingList;
    listBox1.DisplayMember = "MyField";

But that's not automatic. As I understand, sorting should happen whenever the field behind the DisplayMember is updated, through an event or something like that...

See my complete test anyway:

public partial class Form1 : Form
{
    public BindingList<ABC> myBindingList = new BindingList<ABC>();

    public Form1() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
        myBindingList.Add(new ABC("zzz"));
        myBindingList.Add(new ABC("aaa"));
    }

    private void button2_Click(object sender, EventArgs e) {
        myBindingList[0].MyField = "ccc"; // was "zzz"
        myBindingList[1].MyField = "ddd"; // was "aaa"

        listBox1.DataSource = null;
        listBox1.DataSource = myBindingList;
        listBox1.DisplayMember = "MyField";
    }

    private void Form1_Load(object sender, EventArgs e) {
        listBox1.DataSource = myBindingList;
        listBox1.DisplayMember = "MyField";

    }
}

public class ABC  {
    public string MyField { get; set; } 
    public ABC(string val) {
        MyField = val;
    }
}
窝囊感情。 2024-10-01 17:38:13

列表控件上的 LVS_SORT 样式应该可以工作,但你说它不行。我会仔细检查它是否已应用。我从来没有遇到过自排序下拉列表控件的任何问题。请注意,我们所说的是列表控件,而不是列表视图控件。

The LVS_SORT style on the list control should work, but you say it doesn't. I would double check that it is applied. I've never had any trouble with a self-sorting drop-down list control. Note this is a list control we're speaking of, not a listview control.

薯片软お妹 2024-10-01 17:38:13

我通过创建一个新类 BindingSortingList 来实现此目的,该类继承自 BindingList。在其中我重写了所有必要的方法,例如ApplySortCore()和RemoveSortCore()。当您应用排序时,您基本上必须在内部将其复制到具有排序功能的标准列表,对其进行排序,然后将其复制回“此”列表。这看起来很疯狂,但现在我有一个可重用的类用于此目的。

I did this by creating a new class, BindingSortingList, which inherited from BindingList. In it I overrode all the necessary methods, like ApplySortCore() and RemoveSortCore(). When you apply the sort, internally you basically have to copy it to a standard list, which has sorting ability, sort it, then copy it back into the "this" list. It seems crazy but now I have a reusable class for this purpose.

难以启齿的温柔 2024-10-01 17:38:13
 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      //Sorting function
    }

这个怎么办??

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      //Sorting function
    }

What about this??

讽刺将军 2024-10-01 17:38:13
<ListBox x:Name="UsersList"  SelectionChanged="SelectionChngd">
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <Border BorderBrush="Red" BorderThickness="5">
                    <Grid MouseEnter="Grid_MouseEnter"> 
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                            <TextBlock   Text="{Binding Name}"/>
                        <TextBlock Grid.Row="1" Text="{Binding Email}"/>
                    </Grid>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>
<ListBox x:Name="UsersList"  SelectionChanged="SelectionChngd">
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <Border BorderBrush="Red" BorderThickness="5">
                    <Grid MouseEnter="Grid_MouseEnter"> 
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                            <TextBlock   Text="{Binding Name}"/>
                        <TextBlock Grid.Row="1" Text="{Binding Email}"/>
                    </Grid>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>
差↓一点笑了 2024-10-01 17:38:13
namespace SilverlightApplication8
{
    public partial class MainPage : UserControl
    {
        ObservableCollection<UserData> users = new ObservableCollection<UserData>();
        public MainPage()
        {
            Service1Client client = new Service1Client();
            client.GetUsersCompleted += completed;
            client.GetUsersAsync(5);
            InitializeComponent();

            image.Source = new BitmapImage(new Uri(@"c:\1.JPG"));
        }    

        private void completed(object sender, GetUsersCompletedEventArgs e)
        {
            users=e.Result;

            UsersList.ItemsSource = users;
        }

        private void SelectionChngd(object sender, SelectionChangedEventArgs e)
        {
            UserData u= (UserData)(UsersList.SelectedItem);
            DescText.Text = u.Desc;

            image.Source = new BitmapImage(new Uri(@"http://profile.ak.fbcdn.net/hprofile-ak-snc4/49939_713180125_9000_q.jpg"));
        }

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            if (UsersList.SelectedItem != null)
            {
                UserData u = (UserData)(UsersList.SelectedItem);
                DescText.Text = u.Desc;
            }
        }
    }
}
namespace SilverlightApplication8
{
    public partial class MainPage : UserControl
    {
        ObservableCollection<UserData> users = new ObservableCollection<UserData>();
        public MainPage()
        {
            Service1Client client = new Service1Client();
            client.GetUsersCompleted += completed;
            client.GetUsersAsync(5);
            InitializeComponent();

            image.Source = new BitmapImage(new Uri(@"c:\1.JPG"));
        }    

        private void completed(object sender, GetUsersCompletedEventArgs e)
        {
            users=e.Result;

            UsersList.ItemsSource = users;
        }

        private void SelectionChngd(object sender, SelectionChangedEventArgs e)
        {
            UserData u= (UserData)(UsersList.SelectedItem);
            DescText.Text = u.Desc;

            image.Source = new BitmapImage(new Uri(@"http://profile.ak.fbcdn.net/hprofile-ak-snc4/49939_713180125_9000_q.jpg"));
        }

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            if (UsersList.SelectedItem != null)
            {
                UserData u = (UserData)(UsersList.SelectedItem);
                DescText.Text = u.Desc;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文