Moving an item up and down in a WPF list box
Posted
by
DommyCastles
on Stack Overflow
See other posts from Stack Overflow
or by DommyCastles
Published on 2012-09-22T03:34:07Z
Indexed on
2012/09/22
3:37 UTC
Read the original article
Hit count: 82
I have a list box with a bunch of values in it. I also have an UP button and a DOWN button. With these buttons, I would like to move the selected item in the list box up/down. I am having trouble doing this.
Here is my code so far:
private void btnDataUp_Click(object sender, RoutedEventArgs e)
{
int selectedIndex = listBoxDatasetValues.SelectedIndex; //get the selected item in the data list
if (selectedIndex != -1 && selectedIndex != 0) //if the selected item is selected and not at the top of the list
{
//swap items here
listBoxDatasetValues.SelectedIndex = selectedIndex - 1; //keep the item selected
}
}
I do not know how to swap the values! Any help would be GREATLY appreciated!
© Stack Overflow or respective owner