简体   繁体   English

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

[英]How do I build a WPF application where I can drag and drop a user control between windows?

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.我正在构建一个简单的待办事项列表应用程序,我希望能够在桌面上浮动多个列表,我可以在其中标记和管理任务。

The relevant UIElements in my app are:我的应用程序中的相关 UIElements 是:

Window1 (Window) TodoList (User Control) TodoStackCard (User Control) Window1(窗口) 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?我可以纯粹在 C# 和 WPF 中完成这个吗? If so/if not, how?如果是/如果不是,怎么办?

You have to call DoDragDrop to initialize the Drag And Drop framework.您必须调用 DoDragDrop 来初始化拖放框架。 Jaime Rodriguez provides a guide to Drag and Drop here Jaime Rodriguez 在此处提供拖放指南

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.仅供参考,“拖动控件”和执行 Explorer 所做的操作(即拖放,特别是文件)之间存在很大差异。 That's what you'll want to look up, how to do drag and drop from a WPF app to something else.这就是您要查找的内容,如何从 WPF 应用程序拖放到其他内容。 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.您将需要创建数据对象 (IDataObject) 或 WPF 世界中他们称之为的任何东西,然后您需要调用 DoDragDrop(再次,或在 WPF 中与此类似的任何东西)来开始拖动。 Doing what explorer does is also possible, put I suspect you need ot make some lower level calls to accomplish this.做 explorer 做的事情也是可能的,我怀疑你需要做一些较低级别的调用来完成这个。 Take a look at http://www.codeproject.com/KB/wtl/wtl4mfc10.aspx to see the stuff you need ot look for.看看http://www.codeproject.com/KB/wtl/wtl4mfc10.aspx看看你需要寻找的东西。 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. WPF 实际上可能包含所有这些,但如果不是,这些是您需要研究的一些内容,尤其是 IDragSourceHelper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何允许从Windows资源管理器拖放到C#WPF应用程序中? - How can I allow Drag and Drop from Windows Explorer into a C# WPF application? 如何将文件拖放到应用程序中? - How do I drag and drop files into an application? 如何覆盖WPF DataGrid行为以实现拖放到外部应用程序? - How do I override WPF DataGrid behaviour to implement Drag and Drop to an external application? 如何正确允许WPF中的Drag n Drop? - How do I properly allow Drag n Drop in WPF? 如何在C#WPF中拖放两个标签的Exchange数据? - How can I do Exchange data of two labels on drag and drop in C# WPF? 如何在Windows窗体应用程序和WebBrowser控件之间进行通信 - How can I communicate between a windows forms application and a WebBrowser control 将文件拖放到 wpf/C# 应用程序时,如何在 Windows 资源管理器中维护文件顺序? - How do I maintain the file order in Windows Explorer when drag and dropping files into wpf/C# application? 如何将WPF用户控件创建为dll,然后在另一个WPF应用程序中看到它? - How can I create WPF User Control as dll and then see it in another WPF application? 如何捕获其他应用程序中的拖放 - How can I capture drag and drop from the other application 如何在WPF上画一条线从拖拽源拖拽到鼠标上? - How do I draw a line on WPF drag and drop to the mouse from the drag source?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM