Moving ListBoxItem Up/Down in WPF?

If you don't want to implement something complex than the Move Up and Move Down can be handled like this. If the source looks like this.

If you don't want to implement something complex than the Move Up and Move Down can be handled like this. If the source looks like this public ObservableCollection FileNames { get; set; } private void moveUp_Click(object sender, RoutedEventArgs e) { FileClass selectedfile = listBox1. SelectedItem as FileClass; int index = FileNames.

IndexOf(selectedfile); if (index > 0) { FileNames. Remove(selectedfile); FileNames. Insert(index-1, selectedfile); listBox1.

SelectedItem = selectedfile; } } private void moveDown_Click(object sender, RoutedEventArgs e) { FileClass selectedfile = listBox1. SelectedItem as FileClass; int index = FileNames. IndexOf(selectedfile); if (index Count-1) { FileNames.

Remove(selectedfile); FileNames. Insert(index + 1, selectedfile); listBox1. SelectedItem = selectedfile; } } CHANGE Try this code to move items up and down with drag and drop within the ListBox private void listBox1_Drop(object sender, DragEventArgs e) { ListBox parent = sender as ListBox; FileClass data = e.Data.

GetData(typeof(FileClass)) as FileClass; FileClass objectToPlaceBefore = GetObjectDataFromPoint(parent, e. GetPosition(parent)) as FileClass; if (data! = null && objectToPlaceBefore!

= null) { int index = FileNames. IndexOf(objectToPlaceBefore); FileNames. Remove(data); FileNames.

Insert(index, data); listBox1. SelectedItem = data; } } private void listBox1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ListBox parent = sender as ListBox; FileClass data = GetObjectDataFromPoint(parent, e. GetPosition(parent)) as FileClass; if (data!

= null) { DragDrop. DoDragDrop(parent, data, DragDropEffects. Move); } } private static object GetObjectDataFromPoint(ListBox source, Point point) { UIElement element = source.

InputtTest(point) as UIElement; if (element! = null) { object data = DependencyProperty. UnsetValue; while (data == DependencyProperty.

UnsetValue) { data = source. ItemContainerGenerator. ItemFromContainer(element); if (data == DependencyProperty.

UnsetValue) element = VisualTreeHelper. GetParent(element) as UIElement; if (element == source) return null; } if (data! = DependencyProperty.

UnsetValue) return data; } return null; } That should complete the drag'n'drop.

Thanks for your answer. It work perfectly. For drag-and-drop, I'm still working because its on same ListBox, and as Button is using for Up and Down, user can arrange list using drag-and-drop.

– asifabbas Oct 23 '10 at 18:46 Ok, I missunderstood, thought you wanted to drag'n'drop from another control. – Meleak Oct 23 '10 at 18:56 Updated my example to rearrange list with drag and drop – Meleak Oct 23 '10 at 20:28.

Please refer this sample ... may be it can help you c-sharpcorner.com/UploadFile/dpatra/760/....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions