在WPF列表框中上下移动项目

时间:2012-09-22 03:34:08

标签: c# wpf

我有一个列表框,里面有一堆值。我还有一个UP按钮和一个DOWN按钮。 使用这些按钮,我想向上/向下移动列表框中的所选项目。我无法做到这一点。

到目前为止,这是我的代码:

    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
        }


    }

我不知道如何交换价值观!任何帮助都会非常感激!

7 个答案:

答案 0 :(得分:6)

我为此做了一些扩展方法:

    public static void MoveItemUp<T>(this ObservableCollection<T> baseCollection, int selectedIndex)
    {
        //# Check if move is possible
        if (selectedIndex <= 0)
            return;

        //# Move-Item
        baseCollection.Move(selectedIndex - 1, selectedIndex);
    }

    public static void MoveItemDown<T>(this ObservableCollection<T> baseCollection, int selectedIndex)
    {
        //# Check if move is possible
        if (selectedIndex < 0 || selectedIndex + 1 >= baseCollection.Count)
            return;

        //# Move-Item
        baseCollection.Move(selectedIndex + 1, selectedIndex);
    }

    public static void MoveItemDown<T>(this ObservableCollection<T> baseCollection, T selectedItem)
    {
        //# MoveDown based on Item
        baseCollection.MoveItemDown(baseCollection.IndexOf(selectedItem));
    }

    public static void MoveItemUp<T>(this ObservableCollection<T> baseCollection, T selectedItem)
    {
        //# MoveUp based on Item
        baseCollection.MoveItemUp(baseCollection.IndexOf(selectedItem));
    }

没有必要知道ListBox。

答案 1 :(得分:4)

这是最简单的方法,它会触发所有正确的事件,因此您不必担心XAML。 ObservableCollection有一个很好的方法叫做

MoveItem(previousIndex, newIndex)

鉴于你有一个名为DataItemList的ObservableCollection

public void MoveUp()
{
  var currentIndex = DataItemList.SelectedIndex;

  //Index of the selected item
  if (currentIndex > 0)
  {
    int upIndex = currentIndex - 1;

    //move the items
    DataItemList.MoveItem(upIndex,currentIndex);         
  }
}

对于Down,您将获得前一项的索引。

这很简单!

答案 2 :(得分:3)

我会添加评论,但我不能,因为我只有3个声望:/

Peter Hansen的解决方案很棒,但是如果没有选中的元素,down_click会抛出ArgumentOutOfRange异常。这是因为如果没有选择元素,则索引等于-1。

我会像这样编辑down_click:

private void down_click(object sender, RoutedEventArgs e)
{
    if (this.lbItems.SelectedIndex != -1) //Added condition
    {
        var selectedIndex = this.lbItems.SelectedIndex;
        if (selectedIndex + 1 < this.ListItems.Count)
        {
            var itemToMoveDown = this.ListItems[selectedIndex];
            this.ListItems.RemoveAt(selectedIndex);
            this.ListItems.Insert(selectedIndex + 1, itemToMoveDown);
            this.lbItems.SelectedIndex = selectedIndex + 1;
        }
    }
}

答案 3 :(得分:1)

试试这个:

if (listBoxDatasetValues.SelectedItems.Count > 0)
{
    object selected = listBoxDatasetValues.SelectedItem;
    int indx = listBoxDatasetValues.Items.IndexOf(selected);
    int totl = listBoxDatasetValues.Items.Count;

    if (indx == 0)
    {
        listBoxDatasetValues.Items.Remove(selected);
        listBoxDatasetValues.Items.Insert(totl - 1, selected);
        listBoxDatasetValues.SetSelected(totl - 1, true);
    }
    else{
        listBoxDatasetValues.Items.Remove(selected);
        listBoxDatasetValues.Items.Insert(indx - 1, selected);
        listBoxDatasetValues.SetSelected(indx - 1, true);
    }
}

答案 4 :(得分:1)

 if(listBoxDatasetValues.ListIndex > 0)
    {
        // add a duplicate item up in the listbox
        listBoxDatasetValues.AddItem(listBoxDatasetValues.Text, listBoxDatasetValues.ListIndex - 1);
        // make it the current item
        listBoxDatasetValues.ListIndex = (listBoxDatasetValues.ListIndex - 2);
        // delete the old occurrence of this item
        listBoxDatasetValues.RemoveItem(listBoxDatasetValues.ListIndex + 2);
    }

答案 5 :(得分:0)

您可以尝试这样的事情:

向上移动:

if (listboxName.SelectedIndex == -1 || listboxName.SelectedIndex == 0)
    return;

Object select, previous, temp;
select = listboxName.Items[listboxName.SelectedIndex];
previous = listboxName.Items[listboxName.SelectedIndex-1];

temp = select;
select = previous;
previous = temp;

listboxName.Items[listboxName.SelectedIndex] = select;
listboxName.Items[listboxName.SelectedIndex-1] = previous;

listboxName.SelectedIndex--;

向下移动:

if (listboxName.SelectedIndex == -1 || listboxName.SelectedIndex == listboxName.Items.Count-1)
    return;     

Object select, next, temp;
select = listboxName.Items[listboxName.SelectedIndex];
next = listboxName.Items[listboxName.SelectedIndex+1];

temp = select;
select = next;
next = temp;

listboxName.Items[listboxName.SelectedIndex] = select;
listboxName.Items[listboxName.SelectedIndex+1] = next;

listboxName.SelectedIndex++;

答案 6 :(得分:0)

无需使用可观察的集合,您只需依靠ListBox内的集合对象即可使其更简单,这是@Peter Hansen的答案的优化版本:

private void up_click(object sender, RoutedEventArgs e)
{
    var selectedIndex = this.lbItems.SelectedIndex;

    if (selectedIndex > 0)
    {
        var itemToMoveUp = this.lbItems.Items[selectedIndex];
        this.lbItems.Items.RemoveAt(selectedIndex);
        this.lbItems.Items.Insert(selectedIndex - 1, itemToMoveUp);
        this.lbItems.SelectedIndex = selectedIndex - 1;
    }
}

private void down_click(object sender, RoutedEventArgs e)
{
    var selectedIndex = this.lbItems.SelectedIndex;

    if (selectedIndex + 1 < this.lbItems.Items.Count)
    {
        var itemToMoveDown = this.lbItems.Items[selectedIndex];
        this.lbItems.Items.RemoveAt(selectedIndex);
        this.lbItems.Items.Insert(selectedIndex + 1, itemToMoveDown);
        this.lbItems.SelectedIndex = selectedIndex + 1;
    }
}