拖放 WPF 的问题。拖动按钮作为 ListBoxItem 留下空白字段
Problem with drag-and-drop WPF. Dragging button as ListBoxItem leaves blank field
我在两个列表框之间拖动项目时遇到问题。虽然拖动的项目是 ListBoxItem,但效果很好,但拖动按钮会留下空白字段。
我尝试检查拖动项目的类型,但我是 WPF 新手,不知道如何修复它。
这是我的 mainWindow.xaml.cs 文件
private const string formatLista = "Format_Lista";
private const string formatElement = "Format_Element";
private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListBox lbSender = sender as ListBox;
ListBoxItem element = lbSender.GetItemAt(e.GetPosition(lbSender));
if (element == null) return;
var item = VisualTreeHelper.HitTest(lbSender, e.GetPosition(lbSender)).VisualHit;
DataObject dane = new DataObject();
dane.SetData(formatLista, lbSender);
dane.SetData(formatElement, element);
DragDrop.DoDragDrop(lbSender, dane, DragDropEffects.Move | DragDropEffects.Copy);
}
private void ListBox_DragEnter(object sender, DragEventArgs e)
{
if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
e.Effects = DragDropEffects.Copy;
else e.Effects = DragDropEffects.Move;
}
private void ListBox_Drop(object sender, DragEventArgs e)
{
ListBox lbSender = sender as ListBox;
ListBox lbŹródło = e.Data.GetData(formatLista) as ListBox;
ListBoxItem element = e.Data.GetData(formatElement) as ListBoxItem;
if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
{
element = new ListBoxItem() { Content = element.Content };
}
else lbŹródło.Items.Remove(element);
lbSender.Items.Add(element);
}
我在上面的代码中使用了 GetitemAt class
public static class ListBoxExtensions
{
public static ListBoxItem GetItemAt(this ListBox listBox, Point position)
{
var item = VisualTreeHelper.HitTest(listBox, position).VisualHit;
while(item != null && !(item is ListBoxItem))
{
item = VisualTreeHelper.GetParent(item);
}
return item as ListBoxItem;
}
}
这里 xaml 代码:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 1, element 1" />
<ListBoxItem Content="List 1, element 2" />
<ListBoxItem Content="List 1, element 3" />
<ListBoxItem Content="List 1, element 4" />
<ListBoxItem Content="List 1, element 5" />
<Button Content="List 1, button" />
</ListBox>
<ListBox Grid.Column="2"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 2, element 1" />
<ListBoxItem Content="List 2, element 2" />
<ListBoxItem Content="List 2, element 3" />
<ListBoxItem Content="List 2, element 4" />
<ListBoxItem Content="List 2, element 5" />
<Button Content="Lista 2, button" />
</ListBox>
</Grid>
我需要在列表框之间拖动按钮,例如 listboxitem
我认为您最初应该将每个 Button
包装在一个 ListBoxItem
中,如下所示:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 1, element 1" />
<ListBoxItem Content="List 1, element 2" />
<ListBoxItem Content="List 1, element 3" />
<ListBoxItem Content="List 1, element 4" />
<ListBoxItem Content="List 1, element 5" />
<ListBoxItem>
<Button Content="List 1, button" />
</ListBoxItem>
</ListBox>
<ListBox Grid.Column="2"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 2, element 1" />
<ListBoxItem Content="List 2, element 2" />
<ListBoxItem Content="List 2, element 3" />
<ListBoxItem Content="List 2, element 4" />
<ListBoxItem Content="List 2, element 5" />
<ListBoxItem>
<Button Content="List 2, button" />
</ListBoxItem>
</ListBox>
</Grid>
我在两个列表框之间拖动项目时遇到问题。虽然拖动的项目是 ListBoxItem,但效果很好,但拖动按钮会留下空白字段。
我尝试检查拖动项目的类型,但我是 WPF 新手,不知道如何修复它。
这是我的 mainWindow.xaml.cs 文件
private const string formatLista = "Format_Lista";
private const string formatElement = "Format_Element";
private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListBox lbSender = sender as ListBox;
ListBoxItem element = lbSender.GetItemAt(e.GetPosition(lbSender));
if (element == null) return;
var item = VisualTreeHelper.HitTest(lbSender, e.GetPosition(lbSender)).VisualHit;
DataObject dane = new DataObject();
dane.SetData(formatLista, lbSender);
dane.SetData(formatElement, element);
DragDrop.DoDragDrop(lbSender, dane, DragDropEffects.Move | DragDropEffects.Copy);
}
private void ListBox_DragEnter(object sender, DragEventArgs e)
{
if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
e.Effects = DragDropEffects.Copy;
else e.Effects = DragDropEffects.Move;
}
private void ListBox_Drop(object sender, DragEventArgs e)
{
ListBox lbSender = sender as ListBox;
ListBox lbŹródło = e.Data.GetData(formatLista) as ListBox;
ListBoxItem element = e.Data.GetData(formatElement) as ListBoxItem;
if (e.KeyStates.HasFlag(DragDropKeyStates.ControlKey))
{
element = new ListBoxItem() { Content = element.Content };
}
else lbŹródło.Items.Remove(element);
lbSender.Items.Add(element);
}
我在上面的代码中使用了 GetitemAt class
public static class ListBoxExtensions
{
public static ListBoxItem GetItemAt(this ListBox listBox, Point position)
{
var item = VisualTreeHelper.HitTest(listBox, position).VisualHit;
while(item != null && !(item is ListBoxItem))
{
item = VisualTreeHelper.GetParent(item);
}
return item as ListBoxItem;
}
}
这里 xaml 代码:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 1, element 1" />
<ListBoxItem Content="List 1, element 2" />
<ListBoxItem Content="List 1, element 3" />
<ListBoxItem Content="List 1, element 4" />
<ListBoxItem Content="List 1, element 5" />
<Button Content="List 1, button" />
</ListBox>
<ListBox Grid.Column="2"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 2, element 1" />
<ListBoxItem Content="List 2, element 2" />
<ListBoxItem Content="List 2, element 3" />
<ListBoxItem Content="List 2, element 4" />
<ListBoxItem Content="List 2, element 5" />
<Button Content="Lista 2, button" />
</ListBox>
</Grid>
我需要在列表框之间拖动按钮,例如 listboxitem
我认为您最初应该将每个 Button
包装在一个 ListBoxItem
中,如下所示:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 1, element 1" />
<ListBoxItem Content="List 1, element 2" />
<ListBoxItem Content="List 1, element 3" />
<ListBoxItem Content="List 1, element 4" />
<ListBoxItem Content="List 1, element 5" />
<ListBoxItem>
<Button Content="List 1, button" />
</ListBoxItem>
</ListBox>
<ListBox Grid.Column="2"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
AllowDrop="True" DragEnter="ListBox_DragEnter"
Drop="ListBox_Drop">
<ListBoxItem Content="List 2, element 1" />
<ListBoxItem Content="List 2, element 2" />
<ListBoxItem Content="List 2, element 3" />
<ListBoxItem Content="List 2, element 4" />
<ListBoxItem Content="List 2, element 5" />
<ListBoxItem>
<Button Content="List 2, button" />
</ListBoxItem>
</ListBox>
</Grid>