我有ListBox
和DockPanel
。列表框包含应该拖到停靠面板上的项目。我已按照此link实现了这一点。
我有几件事我不明白:
DragDropEffect
属性仅适用于不同的光标设计,或者它具有
更高的目的? :)ListBox
中消失
DockPanel
?这是我的基本拖放代码:
private Point startPosition;
private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startPosition = e.GetPosition(null);
}
private void ListBox_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point currentPosition;
Vector offset;
ListBox listBox;
ListBoxItem item;
Match match;
DataObject dragData;
currentPosition = e.GetPosition(null);
offset = startPosition - currentPosition;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(offset.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(offset.Y) > SystemParameters.MinimumVerticalDragDistance))
{
// Get the data binded to ListBoxItem object, which is "match"
listBox = sender as ListBox;
item = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);
match = (Match)listBox.ItemContainerGenerator.ItemFromContainer(item);
dragData = new DataObject("match", match);
DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move);
}
}
private void DockPanel_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("match") ||
sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
private void DockPanel_Drop(object sender, DragEventArgs e)
{
Match match;
DockPanel matchSlot;
ContentPresenter contentPresenter;
Binding binding;
if (e.Data.GetDataPresent("match"))
{
match = e.Data.GetData("match") as Match;
matchSlot = sender as DockPanel;
contentPresenter = new ContentPresenter();
contentPresenter.ContentTemplate = this.FindResource("MatchTemplate") as DataTemplate;
binding = new Binding();
binding.Source = match;
contentPresenter.SetBinding(ContentPresenter.ContentProperty, binding);
matchSlot.Children.Clear();
matchSlot.Children.Add(contentPresenter);
}
}
感谢所有帮助。
答案 0 :(得分:0)
好的,过了一会儿,我找到了一些答案并自己发现了一些东西。
对于DragDropEffect
枚举,应该使用它有两个原因:
区分是否在代码中移动或复制了项目。它像一面旗帜,应该最常用的方式:
if(e.DragDropEffect == DragDropEffect.Move)
{
...
}
别的......
根据枚举值装饰鼠标光标。这样,它告诉用户他或她是否正在移动或复制项目。
至于拖放可视化,这里是指向包含引用的帖子的链接,这是拖放构建的一个很好的起点:WPF Drag & Drop: How to literally drag an element?