如何构建一个WPF应用程序,我可以在窗口之间拖放用户控件?

时间:2021-05-22 07:25:09

I'm building a simple Todo List application where I want to be able to have multiple lists floating around my desktop that I can label and manage tasks in.

我正在构建一个简单的Todo List应用程序,我希望能够在我的桌面上放置多个列表,我可以在其中标记和管理任务。

The relevant UIElements in my app are:

我的应用程序中的相关UIElements是:

Window1 (Window) TodoList (User Control) TodoStackCard (User Control)

Window1(Window)TodoList(用户控件)TodoStackCard(用户控件)

Window1 looks like this:

Window1看起来像这样:

<Window x:Class="TaskHole.App.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:t="clr-namespace:TaskHole.App.Controls"
    xmlns:tcc="clr-namespace:TaskHole.CustomControls"
    Title="Window1" Width="500" Height="500" Background="Transparent" WindowStyle="None" AllowsTransparency="True" >
    <Canvas Name="maincanvas" Width="500" Height="500" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <ResizeGrip SizeChanged="ResizeGrip_SizeChanged" />
        <t:TodoList Canvas.Top="0" Canvas.Left="0" MinWidth="30" Width="50" Height="500" x:Name="todoList" TaskHover="todoList_TaskHover" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Canvas>
</Window>

TodoList looks like this:

TodoList看起来像这样:

<UserControl x:Class="TaskHole.App.Controls.TodoList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:t="clr-namespace:TaskHole.App.Controls"
    xmlns:tcc="clr-namespace:TaskHole.CustomControls"
    Background="Transparent">
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Stretch" MinWidth="1" Grid.Row="2" Height="Auto" AllowDrop="True">
<ItemsControl Name="todolist" ItemsSource="{Binding}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel Name="stackPanel" VerticalAlignment="Bottom">

                                </VirtualizingStackPanel>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <t:TodoStackCard x:Name="card" TaskHover="card_TaskHover" Orientation="Vertical" VerticalContentAlignment="Top" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
</StackPanel>
</UserControl>

I have multiple instances of these windows, and I want to be able to drag any of the controls between the windows. I have tried using a Thumb control and, while this works, it only allows me to drag a control around the containing canvas.

我有这些窗口的多个实例,我希望能够拖动窗口之间的任何控件。我尝试过使用Thumb控件,虽然这样可行,但它只允许我在包含画布周围拖动一个控件。

How do I mimic the behaviour of, say, Windows Explorer, where I can drag a file outside of the application and onto another application, all the while seeing a ghosted representation of the file under the cursor.

我如何模仿Windows资源管理器的行为,我可以将文件拖到应用程序之外并拖到另一个应用程序上,同时在光标下看到文件的幻影表示。

Can I accomplish this purely in C# and WPF? If so/if not, how?

我可以在C#和WPF中完成这个吗?如果是这样/如果没有,怎么样?

2 个解决方案

#1


2  

You have to call DoDragDrop to initialize the Drag And Drop framework. Jaime Rodriguez provides a guide to Drag and Drop here

您必须调用DoDragDrop来初始化Drag and Drop框架。 Jaime Rodriguez在这里提供拖拽指南

#2


1  

Just as an FYI, there's a big difference to "dragging controls" around, and doing what Explorer does, which is Drag and Drop, specifically with files. That's what you'll want to look up, how to do drag and drop from a WPF app to something else. You'll need something that creates a Data Object (IDataObject) or whatever they call that in WPF world, and then you need to call DoDragDrop (again, or whatever is analogous to this in WPF) to start the dragging. Doing what explorer does is also possible, put I suspect you need ot make some lower level calls to accomplish this. Take a look at http://www.codeproject.com/KB/wtl/wtl4mfc10.aspx to see the stuff you need ot look for. WPF may in fact wrap all this up, but if it doesn't these are some of the things you need to look into, especially IDragSourceHelper.

就像一个FYI一样,“拖动控件”和执行资源管理器的操作有很大的不同,即Drag and Drop,特别是文件。这就是你想要查找的内容,如何从WPF应用程序拖放到其他东西。你需要创建一个数据对象(IDataObject)或者他们在WPF世界中调用它们的东西,然后你需要调用DoDragDrop(再次,或者在WPF中与此类似的任何东西)来开始拖动。做探险家的工作也是可能的,我怀疑你需要做一些较低级别的调用来完成这个。请查看http://www.codeproject.com/KB/wtl/wtl4mfc10.aspx,查看您需要的内容。事实上,WPF可以将所有这些包装起来,但如果不是这些,则需要考虑这些内容,尤其是IDragSourceHelper。

#1


2  

You have to call DoDragDrop to initialize the Drag And Drop framework. Jaime Rodriguez provides a guide to Drag and Drop here

您必须调用DoDragDrop来初始化Drag and Drop框架。 Jaime Rodriguez在这里提供拖拽指南

#2


1  

Just as an FYI, there's a big difference to "dragging controls" around, and doing what Explorer does, which is Drag and Drop, specifically with files. That's what you'll want to look up, how to do drag and drop from a WPF app to something else. You'll need something that creates a Data Object (IDataObject) or whatever they call that in WPF world, and then you need to call DoDragDrop (again, or whatever is analogous to this in WPF) to start the dragging. Doing what explorer does is also possible, put I suspect you need ot make some lower level calls to accomplish this. Take a look at http://www.codeproject.com/KB/wtl/wtl4mfc10.aspx to see the stuff you need ot look for. WPF may in fact wrap all this up, but if it doesn't these are some of the things you need to look into, especially IDragSourceHelper.

就像一个FYI一样,“拖动控件”和执行资源管理器的操作有很大的不同,即Drag and Drop,特别是文件。这就是你想要查找的内容,如何从WPF应用程序拖放到其他东西。你需要创建一个数据对象(IDataObject)或者他们在WPF世界中调用它们的东西,然后你需要调用DoDragDrop(再次,或者在WPF中与此类似的任何东西)来开始拖动。做探险家的工作也是可能的,我怀疑你需要做一些较低级别的调用来完成这个。请查看http://www.codeproject.com/KB/wtl/wtl4mfc10.aspx,查看您需要的内容。事实上,WPF可以将所有这些包装起来,但如果不是这些,则需要考虑这些内容,尤其是IDragSourceHelper。