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

标签 c# wpf windows user-interface

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

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

Window1(窗口) TodoList(用户控件) TodoStackCard(用户控件)

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>

待办事项列表如下所示:

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

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

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

我能否仅使用 C# 和 WPF 完成此操作?如果是/如果不是,如何?

最佳答案

您必须调用 DoDragDrop 来初始化拖放框架。 Jaime Rodriguez 提供拖放指南 here

关于c# - 如何构建可以在窗口之间拖放用户控件的 WPF 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/158372/

相关文章:

c# - 多个线程使用 yield 访问 IEnumerable

c# - 未从 C# 中找到 C++ COM DLL 中的导出函数

wpf - 沿路径对对象进行动画处理

WPF 删除列表框底部的额外空间

python - 无法使用 win32 dir 命令和 Python 子进程查看文件

c# - 将 .XLS 转换为制表符分隔的 .TXT

c# - 拆分字符串

c# - 如何在 View 模型中的 View 上使用路由命令

c++ - 使用自定义安装目录在 Windows 上的 Code::Blocks 中链接 SDL-bgi

python - 在 Windows 中使用 Protocol Buffer