在 WPF 中向上/向下移动 ListBoxItem [英] Moving ListBoxItem Up/Down in WPF
本文介绍了在 WPF 中向上/向下移动 ListBoxItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个包含文件名的列表框.我想给用户一个选项来使用向上/向下按钮和拖放来向上和向下移动文件名.
I've created a listbox which contain filenames. I want to give user a option to move up and down file name using up/down button and using drag and drop.
任何人都知道如何实现此功能.
Anyone has an idea how to achieve this functionality.
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>
推荐答案
如果你不想实现比上移和下移复杂的东西,可以这样处理.如果源看起来像这样
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<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;
}
}
改变
尝试使用此代码通过在 ListBox 中拖放来上下移动项目
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.InputHitTest(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.
这篇关于在 WPF 中向上/向下移动 ListBoxItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文