I have a ListBox
and a DockPanel
. List box contains items that are supposed to be dragged onto the dock panel. I've implemented that by following this link.
我有一个列表框和一个DockPanel。列表框包含应该拖到dock面板上的项。我通过遵循这个链接实现了这一点。
There are a couple of things I do not understand though:
有几件事我不明白:
- While dragging, all I see is a cursor. I'd like to literary see the list item I am dragging to move around with my cursor. How do I do that?
- 拖拽时,我看到的只是一个光标。我想用鼠标拖动列表项来移动。我该怎么做呢?
- Is the
DragDropEffect
property only for the different cursor design or it has a higher purpose? :) - DragDropEffect属性仅用于不同的光标设计,还是有更高的用途?:)
- How do I make list item disappear from the
ListBox
once it is dropped onto theDockPanel
? - 当列表项被放到DockPanel上时,如何使列表项从列表框中消失?
- I'd like to enforce some animation on the items that I drag, like glow once it is dropped. Which trigger/setter should I use for that?
- 我想在拖拽的项目上强制执行一些动画,比如拖拽后的glow。我应该使用哪一个触发器/setter ?
Here's my code for basic dragging and dropping:
下面是我的基本拖放代码:
Code-behind for the ListBox part
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);
}
}
Code-behind for the DockPanel part
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);
}
}
Thanks for all the help.
谢谢你的帮助。
1 个解决方案
#1
0
Ok, after a while I found some answers and discovered a few things on my own.
好吧,过了一会儿我找到了一些答案,发现了一些我自己的东西。
As for the DragDropEffect
enum, it should be used for two reasons:
对于DragDropEffect enum,它的使用应该有两个原因:
-
To distinguish if the item is moved or copied in the code. It serves like a flag and should be used most commonly like this:
区分在代码中移动或复制项目。它就像一面旗帜,最常见的用法是:
if (e.DragDropEffect == DragDropEffect.Move)
{
...
}
else ...如果(e。DragDropEffect == == dragdropeffecet . move){…其他}…
-
To decorate the mouse cursor based on the enum value. This way it tells the user if he or she is moving or copying the item.
根据枚举值装饰鼠标光标。通过这种方式,它会告诉用户他或她正在移动或复制项目。
As for the drag and drop visualization here's a link to the post containing the reference which is an excellent starting point for drag and drop to build on: WPF Drag & Drop: How to literally drag an element?
对于拖放可视化来说,这里有一个指向包含引用的post的链接,这是在WPF拖放上构建拖放的一个很好的起点:如何真正地拖放一个元素?
#1
0
Ok, after a while I found some answers and discovered a few things on my own.
好吧,过了一会儿我找到了一些答案,发现了一些我自己的东西。
As for the DragDropEffect
enum, it should be used for two reasons:
对于DragDropEffect enum,它的使用应该有两个原因:
-
To distinguish if the item is moved or copied in the code. It serves like a flag and should be used most commonly like this:
区分在代码中移动或复制项目。它就像一面旗帜,最常见的用法是:
if (e.DragDropEffect == DragDropEffect.Move)
{
...
}
else ...如果(e。DragDropEffect == == dragdropeffecet . move){…其他}…
-
To decorate the mouse cursor based on the enum value. This way it tells the user if he or she is moving or copying the item.
根据枚举值装饰鼠标光标。通过这种方式,它会告诉用户他或她正在移动或复制项目。
As for the drag and drop visualization here's a link to the post containing the reference which is an excellent starting point for drag and drop to build on: WPF Drag & Drop: How to literally drag an element?
对于拖放可视化来说,这里有一个指向包含引用的post的链接,这是在WPF拖放上构建拖放的一个很好的起点:如何真正地拖放一个元素?