在WPF中向上/向下移动ListBoxItem

在WPF中向上/向下移动ListBoxItem,wpf,drag-and-drop,listbox,Wpf,Drag And Drop,Listbox,我创建了一个包含文件名的列表框。我想给用户一个选项,使用向上/向下按钮和拖放来上下移动文件名 任何人都知道如何实现此功能 XAML代码: <ListBox Grid.Column="0" Name="listBox1" AllowDrop="True" Drop="listBox1_Drop" /> <StackPanel Grid.Column="1" HorizontalAlignment="Stretch" Vert

我创建了一个包含文件名的列表框。我想给用户一个选项,使用向上/向下按钮和拖放来上下移动文件名

任何人都知道如何实现此功能

XAML代码:

<ListBox
    Grid.Column="0"
    Name="listBox1"
    AllowDrop="True"
    Drop="listBox1_Drop"
/>
<StackPanel
    Grid.Column="1"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Center">
    <Button
        Name="moveUp"
        Content="Ç"
        FontFamily="Wingdings 3"
        Margin="3,3,3,3"
        Click="moveUp_Click" />
    <Button
        Name="moveDown"
        FontFamily="Wingdings 3"
        Content="È"
        Margin="3,3,3,3" />
</StackPanel>

请参考此示例。。。也许它能帮助你


如果您不想实现复杂的操作,那么上移和下移可以这样处理。如果源代码看起来像这样

public ObservableCollection<FileClass> 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 < FileNames.Count-1)
    {
        FileNames.Remove(selectedfile);
        FileNames.Insert(index + 1, selectedfile);
        listBox1.SelectedItem = selectedfile;
    }
}

这将完成拖放操作。

如果要绑定按钮,可以尝试以下操作:

private void MoveItemUp()
    {
        if (SelectedGroupField != null)
        {
            List<string> tempList = AvailableGroupField;
            string selectedItem = SelectedGroupField;

            int currentIndex = tempList.IndexOf(selectedItem);

            if (currentIndex > 0)
            {
                tempList.Remove(selectedItem);
                tempList.Insert(currentIndex - 1, selectedItem);
                AvailableGroupField = null;
                AvailableGroupField = tempList;
                SelectedGroupField = AvailableGroupField.Single(p => p == selectedItem);
            }
        }
    }

    private void MoveItemDown()
    {
        if (SelectedGroupField != null)
        {
            List<string> tempList = AvailableGroupField;
            string selectedItem = SelectedGroupField;

            int currentIndex = tempList.IndexOf(selectedItem);

            if (currentIndex < (tempList.Count - 1))
            {
                tempList.Remove(selectedItem);
                tempList.Insert(currentIndex + 1, selectedItem);
                AvailableGroupField = null;
                AvailableGroupField = tempList;
                SelectedGroupField = AvailableGroupField.Single(p => p == selectedItem);
            }
        }        
    }
private void MoveItemUp()
{
如果(SelectedGroupField!=null)
{
列表模板列表=AvailableGroupField;
字符串selectedItem=SelectedGroupField;
int currentIndex=templast.IndexOf(selectedItem);
如果(当前索引>0)
{
移除(选择编辑项);
插入(currentIndex-1,selectedItem);
AvailableGroupField=null;
AvailableGroupField=tempList;
SelectedGroupField=AvailableGroupField.Single(p=>p==selectedItem);
}
}
}
私有void MoveItemDown()
{
如果(SelectedGroupField!=null)
{
列表模板列表=AvailableGroupField;
字符串selectedItem=SelectedGroupField;
int currentIndex=templast.IndexOf(selectedItem);
如果(当前索引<(templast.Count-1))
{
移除(选择编辑项);
插入(currentIndex+1,selectedItem);
AvailableGroupField=null;
AvailableGroupField=tempList;
SelectedGroupField=AvailableGroupField.Single(p=>p==selectedItem);
}
}        
}

谢谢您的回答。它工作得很好。对于拖放,我仍然在工作,因为它在同一个列表框中,而按钮用于上下,用户可以使用拖放来排列列表。好的,我理解错了,以为您想从另一个控件中拖动“n”drop。更新了我的示例,用拖放重新排列列表
private void MoveItemUp()
    {
        if (SelectedGroupField != null)
        {
            List<string> tempList = AvailableGroupField;
            string selectedItem = SelectedGroupField;

            int currentIndex = tempList.IndexOf(selectedItem);

            if (currentIndex > 0)
            {
                tempList.Remove(selectedItem);
                tempList.Insert(currentIndex - 1, selectedItem);
                AvailableGroupField = null;
                AvailableGroupField = tempList;
                SelectedGroupField = AvailableGroupField.Single(p => p == selectedItem);
            }
        }
    }

    private void MoveItemDown()
    {
        if (SelectedGroupField != null)
        {
            List<string> tempList = AvailableGroupField;
            string selectedItem = SelectedGroupField;

            int currentIndex = tempList.IndexOf(selectedItem);

            if (currentIndex < (tempList.Count - 1))
            {
                tempList.Remove(selectedItem);
                tempList.Insert(currentIndex + 1, selectedItem);
                AvailableGroupField = null;
                AvailableGroupField = tempList;
                SelectedGroupField = AvailableGroupField.Single(p => p == selectedItem);
            }
        }        
    }