简体   繁体   中英

How to add an item to the top of a ListBox

In my application I am adding items to a ListBox in C#. I am adding the collection of items which are held in a StackPanel using the code:

ListBoxControl.Items.Add(stackPanel);

But this adds the StackPanel to the bottom of the ListBox , is there any way of adding the StackPanel to the top rather than the bottom?

(It is not possible to add the items to the ListBox in reverse order.)

You can use the ItemCollection.Insert Method

For example

myListbox.Items.Insert(0, myItem);

You can just use the Insert method as Matthew mentioned before or just using Reverse().

ListBoxControl.Items.Add(stackPanel);
ListBoxControl.Items.Reverse();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM