Moving ListBox Items UP Or Down In WPF

Introduction

In this article I have taken one list box and bound it with a list item. Then added two buttons in the form to rearrange the list box items up and down.

The XAML code is given below.

<Window x:Class="MVVM_Application.Views.Preference.Preference1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Preference" Height="673" Width="662" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" BorderBrush="Black" OpacityMask="Black" Background="Black"
xmlns:w="clr-namespace:MVVM_Application.Views.Preference" Initialized="Window_Initialized">

<Grid>
<ListBox Canvas.Left="76" Canvas.Top="62" Height="323" Name="lbxSportList" Width="212" ItemsSource="{Binding Path=Selections}" DisplayMemberPath="SelectionName"/>

<Button Canvas.Left="294" Canvas.Top="175" Height="34" Name="btnUp" Width="49" Click="btnUp_Click">
<Image Height="34" Name="image1" Stretch="Fill" Width="49" Source="/MVVM%20Application;component/Views/Images/arrow-31-512.png"/>
</Button>
<Button Height="34" Name="btnDown" Width="49" Canvas.Left="294" Canvas.Top="215" Click="btnDown_Click">
<Image Height="34" Name="image2" Stretch="Fill" Width="49" Source="/MVVM%20Application;component/Views/Images/arrow-31-512qwq.png" OpacityMask="Black" />
</Button>

</Grid>

To move the list box items up, I have wiriten the following code in the click event of button control in the code behind.

Code behind Code

private void btnUp_Click(object sender, RoutedEventArgs e)
{
    if (this.lbxSportList.SelectedItems.Count <= 0)
        return;
    if (this.lbxSportList.SelectedIndex > 0)
    {
        List<MVVM_Application.Entity.Model.Preference.EventType> EventTypes = (List<MVVM_Application.Entity.Model.Preference.EventType>)this.lbxSportList.ItemsSource;

        //Getting Item from Itemsource of listbox
        MVVM_Application.Entity.Model.Preference.EventType ETU =  
            (MVVM_Application.Entity.Model.Preference.EventType)this.lbxSportList.Items[this.lbxSportList.SelectedIndex - 1];
        MVVM_Application.Entity.Model.Preference.EventType ETD =
           (MVVM_Application.Entity.Model.Preference.EventType)this.lbxSportList.Items[this.lbxSportList.SelectedIndex];

        //Removing items from item source
        EventTypes.Remove(ETU);
        EventTypes.Remove(ETD);

        //Adding items from item source
        ETU.Priority = ETU.Priority + 1;
        ETD.Priority = ETD.Priority - 1;
        EventTypes.Add(ETU);
        EventTypes.Add(ETD);
        //Shorting ListBox Items
        lbxSportList.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Priority",
             System.ComponentModel.ListSortDirection.Ascending));

    }
}

To move the list box items down, I have wiriten the following code in the click event of the button control in the code behind.

Code behind Code

private void btnDown_Click(object sender, RoutedEventArgs e)
{
    if (this.lbxSportList.SelectedItems.Count <= 0)
        return;
    if (this.lbxSportList.SelectedIndex >= 0 && this.lbxSportList.SelectedIndex<this.lbxSportList.Items.Count-1)
    {
        List<MVVM_Application.Entity.Model.Preference.EventType> EventTypes =
             (List<MVVM_Application.Entity.Model.Preference.EventType>)this.lbxSportList.ItemsSource;
        //Getting Item from Itemsource of listbox
        MVVM_Application.Entity.Model.Preference.EventType ETU =
             (MVVM_Application.Entity.Model.Preference.EventType)this.lbxSportList.Items[this.lbxSportList.SelectedIndex];
        MVVM_Application.Entity.Model.Preference.EventType ETD =
            (MVVM_Application.Entity.Model.Preference.EventType)this.lbxSportList.Items[this.lbxSportList.SelectedIndex + 1];

        //Removing items from item source
        EventTypes.Remove(ETU);
        EventTypes.Remove(ETD);

        //Adding items from item source
        ETU.Priority = ETU.Priority + 1;
        ETD.Priority = ETD.Priority - 1;

        EventTypes.Add(ETU);
        EventTypes.Add(ETD);

        //Shorting ListBox Items
        lbxSportList.Items.SortDescriptions.Add(
            new System.ComponentModel.SortDescription("Priority",
            System.ComponentModel.ListSortDirection.Ascending));
    }
}