PanelDragDropTarget 和 ListBoxDragDropTarget:到底拖动了什么?

发布于 2024-12-03 14:49:27 字数 2108 浏览 0 评论 0原文

我的 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

因此,放置的对象可以是 PersonPersonControl,具体取决于它的来源。

我真的想在所有情况下拖放 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-12-10 14:49:27

您对 PanelDragDropTargets 和 ListBoxDragDropTargets 传输放置对象的方式之间的差异完全正确。在 2 个 ListBoxDragDropTargets 之间拖动时,您将传输绑定到控件的数据片段,而在 2 个 PanelDragDropTargets 之间拖动则传输“拾取”的 UIElement。

我发现解决此问题的最佳方法是从 PanelDragDropTarget 派生一个新类,该类期望将一段数据而不是 UIElement 拖放到其上。新类将数据对象存储在类/控件的 DataContext 中。这允许与您的代码类似的代码运行。

类:

public class ElementDragDropTarget : PanelDragDropTarget
{
    protected override bool CanAddItem(Panel itemsControl, object data)
    {
        return true;
    }

    protected override void InsertItem(Panel itemsControl, int index, object data)
    {
        itemsControl.DataContext = data;
    }

    protected override bool CanRemove(Panel itemsControl)
    {
        return true;
    }

    protected override void RemoveItem(Panel itemsControl, object data)
    {
        itemsControl.DataContext = null;
    }

    protected override void RemoveItemAtIndex(Panel itemsControl, int index)
    {
        itemsControl.DataContext = null;
    }

    protected override object ItemFromContainer(Panel itemsControl, UIElement itemContainer)
    {
        return itemsControl.DataContext;
    }
}

编辑您的代码:

<local:ElementDragDropTarget AllowDrop="True" AllowedSourceEffects="Copy,Move" Drop="PanelDragDropTarget_Current_Drop">
    <Grid>
        <ctl:PersonControl Margin="3,3,3,3" x:Name="pcCurrent"></ctl:PersonControl>
    </Grid>
</local:ElementDragDropTarget >

我意识到此解决方案仅允许将一项拖入 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:

public class ElementDragDropTarget : PanelDragDropTarget
{
    protected override bool CanAddItem(Panel itemsControl, object data)
    {
        return true;
    }

    protected override void InsertItem(Panel itemsControl, int index, object data)
    {
        itemsControl.DataContext = data;
    }

    protected override bool CanRemove(Panel itemsControl)
    {
        return true;
    }

    protected override void RemoveItem(Panel itemsControl, object data)
    {
        itemsControl.DataContext = null;
    }

    protected override void RemoveItemAtIndex(Panel itemsControl, int index)
    {
        itemsControl.DataContext = null;
    }

    protected override object ItemFromContainer(Panel itemsControl, UIElement itemContainer)
    {
        return itemsControl.DataContext;
    }
}

Edits to your code:

<local:ElementDragDropTarget AllowDrop="True" AllowedSourceEffects="Copy,Move" Drop="PanelDragDropTarget_Current_Drop">
    <Grid>
        <ctl:PersonControl Margin="3,3,3,3" x:Name="pcCurrent"></ctl:PersonControl>
    </Grid>
</local:ElementDragDropTarget >

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文