【问题标题】:WPF Drag & Drop: How to literally drag an element?WPF拖放:如何从字面上拖动元素?
【发布时间】:2010-12-25 18:05:45
【问题描述】:

假设我有一个带有一些项目的ListBox,并说我为该列表实现了拖放功能。如果我想从该列表框中拖动一个项目,我该如何实际移动拖动的项目?

我想实现将列表框项目置于鼠标光标下的效果,并且当我在窗口上拖动它时能够随之移动。通过遵循这个example,我得到的只是基于DragDropEffects 枚举选项的光标更改。

【问题讨论】:

    标签: wpf drag-and-drop


    【解决方案1】:

    这通常使用装饰器来完成。示例见here

    【讨论】:

    • 谢谢肯特。最后,我跟着 Bea 的拖放实现beacosta.com/blog/?p=53。您的参考使我找到了他的帖子。
    • 链接已损坏。你能更新你的答案吗?
    【解决方案2】:

    调整大小.Xaml:

    <UserControl x:Class="ERDesign.Resize"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ERDesign="clr-namespace:ERDesign"
             mc:Ignorable="d"
             d:DesignHeight="50" d:DesignWidth="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <UserControl.Resources>
            <ControlTemplate x:Key="TemplateResize" TargetType="{x:Type Control}">
                <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                    <Rectangle Margin="-6 -6 -6 -6" Stroke="#FF555555" StrokeThickness="1" StrokeDashArray="4.0 4.0" SnapsToDevicePixels="True"></Rectangle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-10 -10 0 0" Cursor="SizeNWSE" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0 -10 0 0" Cursor="SizeNS" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0 -10 -10 0" Cursor="SizeNESW" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Left" VerticalAlignment="Center" Margin="-10 0 0 0" Cursor="SizeWE" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0 0 -10 0" Cursor="SizeWE" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="-10 0 0 -10" Cursor="SizeNESW" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0 0 0 -10" Cursor="SizeNS" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                    <ERDesign:ResizeHandle HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0 0 -10 -10" Cursor="SizeNWSE" SnapsToDevicePixels="True"></ERDesign:ResizeHandle>
                </Grid>
            </ControlTemplate>
        </UserControl.Resources>
        <Control HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Template="{StaticResource TemplateResize}" SnapsToDevicePixels="True"></Control>
    </UserControl>
    

    ResizeHandle.Xaml

    <UserControl x:Class="ERDesign.ResizeHandle"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="9" d:DesignWidth="9" Width="9" Height="9">
        <UserControl.Resources>
            <ControlTemplate x:Key="TemplateResizeHandle" TargetType="{x:Type Thumb}">
                <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                    <Ellipse Stroke="#FF2F592F" Stretch="Fill">
                        <Ellipse.Fill>
                            <RadialGradientBrush GradientOrigin="0.3,0.3">
                                <GradientStop Color="#FFF2FCF2" Offset="0"></GradientStop>
                                <GradientStop Color="#FF4ECB4E" Offset="1"></GradientStop>
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                </Grid>
            </ControlTemplate>
        </UserControl.Resources>
        <Thumb HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Template="{StaticResource TemplateResizeHandle}" SnapsToDevicePixels="True" DragDelta="Thumb_DragDelta"></Thumb>
    </UserControl>
    

    ResizeHandle.Xaml.cs

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    
    namespace ERDesign
    {
        public partial class ResizeHandle : UserControl
        {
            public ResizeHandle()
            {
                InitializeComponent();
            }
    
            private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
            {
                ResizeHandle resizehandle = (sender as Thumb).Parent as ResizeHandle;
                Resize resize = (resizehandle.DataContext as Control).Parent as Resize;
                UserControl userctrl = (resize.DataContext as ContentControl).Parent as UserControl;
    
                if (userctrl == null)
                {
    
                }
                else
                {
                    double X;
                    double Y;
    
                    switch (this.HorizontalAlignment)
                    {
                        case HorizontalAlignment.Left:
                            X = Math.Min(e.HorizontalChange, userctrl.ActualWidth - userctrl.MinWidth);
                            Canvas.SetLeft(userctrl, Canvas.GetLeft(userctrl) + X);
                            userctrl.Width = userctrl.Width - X;
                            break;
                        case HorizontalAlignment.Right:
                            X = Math.Min(-e.HorizontalChange, userctrl.ActualWidth - userctrl.MinWidth);
                            userctrl.Width = userctrl.Width - X;
                            break;
                        default:
                            break;
                    }
    
                    switch (this.VerticalAlignment)
                    {
                        case VerticalAlignment.Top:
                            Y = Math.Min(e.VerticalChange, userctrl.ActualHeight - userctrl.MinHeight);
                            Canvas.SetTop(userctrl, Canvas.GetTop(userctrl) + Y);
                            userctrl.Height = userctrl.Height - Y;
                            break;
                        case VerticalAlignment.Bottom:
                            Y = Math.Min(-e.VerticalChange, userctrl.ActualHeight - userctrl.MinHeight);
                            userctrl.Height = userctrl.Height - Y;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-16
      • 2012-09-17
      • 1970-01-01
      • 2010-09-16
      • 2020-08-05
      • 2012-01-06
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多