PanelDragDropTarget 和 ListBoxDragDropTarget:到底拖动了什么?
我的 Silverlight 4 应用程序允许从 ListBoxDragDropTarget 拖动到 PanelDragDropTarget。
该应用程序具有代表人的模型的 Person
对象,以及将 Person
作为其 DataContext 的 PersonControl
用户控件。
作为参考,相关代码是:
<toolkit:ListBoxDragDropTarget x:Name="dtListBox" Grid.Row="2" AllowedSourceEffects="Copy" AllowDrop="True"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Top" VerticalContentAlignment="Stretch">
<!-- FilteredMembers is of type ObservableCollection<Person> -->
<ListBox ItemsSource="{Binding FilteredMembers}"
MinWidth="42"
MinHeight="42">
<ListBox.ItemTemplate>
<DataTemplate>
<my:PersonControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</toolkit:ListBoxDragDropTarget>
到目前为止
<toolkit:PanelDragDropTarget AllowDrop="True" AllowedSourceEffects="Copy,Move"
Drop="PanelDragDropTarget_Current_Drop">
<StackPanel>
<ctl:PersonControl Margin="3,3,3,3" x:Name="pcCurrent"></ctl:PersonControl>
</StackPanel>
</toolkit:PanelDragDropTarget>
,一切都很好。当我拖动到 PanelDragDropTarget
时,我得到一个 Person
。
不过,我也允许从 PanelDragDropTarget
拖动到另一个 PanelDragDropTarget
。在这种情况下,放置的对象不是 Person
,而是 PersonControl
。
因此,放置的对象可以是 Person
或 PersonControl
,具体取决于它的来源。
我真的想在所有情况下拖放 Person
对象,而不是在 PersonControl
周围移动。如何修改我的 PanelDragDropTarget
以便拖动时拉动 Person
而不是 PersonControl
?
我已经审查过这个非常类似的问题:
从ListBoxDragDropTarget拖/放到PanelDragDropTarget
但是不明白这如何解决问题。
My Silverlight 4 app allows dragging from a ListBoxDragDropTarget to a PanelDragDropTarget.
The application has Person
objects that are models representing people, and PersonControl
user controls that have a Person
as their DataContext.
For reference the relevant code is:
<toolkit:ListBoxDragDropTarget x:Name="dtListBox" Grid.Row="2" AllowedSourceEffects="Copy" AllowDrop="True"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Top" VerticalContentAlignment="Stretch">
<!-- FilteredMembers is of type ObservableCollection<Person> -->
<ListBox ItemsSource="{Binding FilteredMembers}"
MinWidth="42"
MinHeight="42">
<ListBox.ItemTemplate>
<DataTemplate>
<my:PersonControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</toolkit:ListBoxDragDropTarget>
and
<toolkit:PanelDragDropTarget AllowDrop="True" AllowedSourceEffects="Copy,Move"
Drop="PanelDragDropTarget_Current_Drop">
<StackPanel>
<ctl:PersonControl Margin="3,3,3,3" x:Name="pcCurrent"></ctl:PersonControl>
</StackPanel>
</toolkit:PanelDragDropTarget>
So far, so good. When I drag to the PanelDragDropTarget
, I get a Person
.
However, I also allow dragging from the PanelDragDropTarget
to another PanelDragDropTarget
. In that case, rather than a Person
, the dropped object is a PersonControl
.
So, the dropped object can be either a Person
or a PersonControl
depending on where it comes from.
I really want to drag and drop Person
objects in all cases, rather than moving around PersonControl
. How can I modify my PanelDragDropTarget
so that dragging pulls the Person
rather than the PersonControl
?
I have reviewed this very similar question:
Drag/drop from ListBoxDragDropTarget to PanelDragDropTarget
but do not understand how that solves the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对 PanelDragDropTargets 和 ListBoxDragDropTargets 传输放置对象的方式之间的差异完全正确。在 2 个 ListBoxDragDropTargets 之间拖动时,您将传输绑定到控件的数据片段,而在 2 个 PanelDragDropTargets 之间拖动则传输“拾取”的 UIElement。
我发现解决此问题的最佳方法是从 PanelDragDropTarget 派生一个新类,该类期望将一段数据而不是 UIElement 拖放到其上。新类将数据对象存储在类/控件的 DataContext 中。这允许与您的代码类似的代码运行。
类:
编辑您的代码:
我意识到此解决方案仅允许将一项拖入 ElementDragDropTarget。我认为这就是您想要做的,因为如果您想将多个数据对象放入其中,您可能应该使用另一个 ListBoxDragDropTarget (特别是因为您只是使用 StackPanel)。
You are exactly right about the difference between the way PanelDragDropTargets and ListBoxDragDropTargets transfer dropped objects. When dragging between 2 ListBoxDragDropTargets you are transferring the piece of data that is bound to the control, whereas dragging between 2 PanelDragDropTargets transfers the UIElement that is "picked up."
The best way I have found to get around this is to derive a new class from PanelDragDropTarget that expects a piece of data to be dropped on it instead of a UIElement. The new class stores the data object in the class'/control's DataContext. This allows code that is similar to yours to work.
The class:
Edits to your code:
I realize this solution only allows one item to be dragged into the ElementDragDropTarget. I assume that is what you want to do because if you wanted to drop multiple data objects into it you should probably just use another ListBoxDragDropTarget (especially since you're just using a StackPanel).