WPF拖放控件

29 篇文章 0 订阅
19 篇文章 1 订阅

拖放官方文档

拖放操作通常涉及两个参与方:拖动对象所源自的拖动源和接收放置对象的拖放目标。 拖动源和放置目标可能是相同应用程序或不同应用程序中的 UI 元素。

我这里实现的是对TabControl的Tab页面进行拖放,以达成类似Chrome浏览器的拖放功能。

对拖放相关事件研究后发现,最终只需要在拖动源事件处理程序中调用 DoDragDrop 方法启动拖动操作,然后在拖放目标上,响应Drop事件。启动拖放操作的拖动源事件通常是 MouseMove事件,另外需要判断鼠标左键是否为按下状态。另外,最好判断一下鼠标移动的距离是否大于最小拖动距离(在我没有添加这个检查之前,双击也会触发拖动事件)。

除了正常拖动(将Tab页从一个TabControl 拖动到另一个 TabControl 中),考虑将Tab页拖动到窗口之外的情况(浏览器在这种情况下会创建一个新的窗口来显示这个Tab页),我这里采用类似的方式,创建一个预设的子窗口来显示该Tab页面。

此外,当Tab页面在原TabControl内部拖动时,根据拖动位置,调整Tab页面的顺序。

我这里对原生的TabControl进行了继承封装了几个响应事件,轻量化的实现了拖放效果。

TabControlDragable.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WpfLibrary;

public class TabControlDragable : TabControl
{
    public TabControlDragable()
    {
        AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseLeftButtonDown), true);
        AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseLeftButtonUp), true);
        AddHandler(MouseMoveEvent, new MouseEventHandler(OnMouseMove), true);

        AddHandler(DropEvent, new DragEventHandler(OnDrop), true);
        MinWidth = 100;
        MinHeight = 100;
        AllowDrop = true;
    }
    private bool IsDraging = false;
    private object? DragData;
    private Point DragStartPosition;

    private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Point p = e.GetPosition(this);
        DragData = DragUtility.GetDataObjectFromItemsControl(this, p);
        if (DragData != null)
        {
            DragStartPosition = p;
        }
        e.Handled = true;
    }
    private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        ResetState();
        e.Handled = true;
    }
    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (DragData == null || DragData is not TabItem)
        {
            return;
        }
        Point currentPosition = e.GetPosition(this);
        if (e.LeftButton == MouseButtonState.Pressed && !IsDraging
            && ((Math.Abs(currentPosition.X - DragStartPosition.X) > SystemParameters.MinimumHorizontalDragDistance) || (Math.Abs(currentPosition.Y - DragStartPosition.Y) > SystemParameters.MinimumVerticalDragDistance)))
        {
            DataObject dataObject = new DataObject();
            dataObject.SetData("DragData", DragData);
            var effects = DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Move);
            if (effects == DragDropEffects.None)
            {
                if (Items.Count != 1)
                {
                    var tabItem = DragData as TabItem;
                    var parentWindow = DragUtility.GetParentWindow(tabItem);
                    Items.Remove(tabItem);
                    ChildWindow newChildWindow = new ChildWindow();
                    if (parentWindow is ChildWindow)
                    {
                        newChildWindow.Owner = parentWindow.Owner;
                    }
                    else
                    {
                        newChildWindow.Owner = parentWindow;
                    }
                    newChildWindow.DataContext = parentWindow?.DataContext;
                    var tabControl = new TabControlDragable();
                    newChildWindow.MyGrid.Children.Add(tabControl);
                    tabControl.Items.Add(tabItem);
                    newChildWindow.Show();
                }
            }
            ResetState();
        }
        e.Handled = true;
    }
    private int MeasureIndex(TabControl targetTabControl, Point point)
    {
        double preWidth = 0.0;
        for (var i = 0; i < targetTabControl.Items.Count; i++)
        {
            var item = targetTabControl.Items[i] as TabItem;
            if (item == null)
            {
                continue;
            }
            if (point.Y > item.ActualHeight)
            {
                return targetTabControl.Items.Count;
            }
            if (point.X < preWidth + item.ActualWidth / 2)
            {
                return i;
            }
            else
            {
                preWidth += item.ActualWidth;
            }
        }
        return targetTabControl.Items.Count;
    }
    private void OnDrop(object sender, DragEventArgs e)
    {
        var tabItem = e.Data.GetData("DragData") as TabItem;
        var parentTabControl = tabItem?.Parent as TabControl;
        if (tabItem == null || parentTabControl == null)
        {
            return;
        }
        var point = e.GetPosition(this);
        int index = MeasureIndex(this, point);
        if (this != parentTabControl)
        {
            var parentWindow = DragUtility.GetParentWindow(parentTabControl);
            if (parentTabControl.Items.Count == 1)
            {
                var childWindow = parentWindow as ChildWindow;
                if (childWindow != null)
                {
                    parentTabControl.Items.Remove(tabItem);
                    this.Items.Insert(index, tabItem);
                    this.SelectedItem = tabItem;
                    childWindow?.Close();
                }
            }
            else
            {
                parentTabControl.Items.Remove(tabItem);
                this.Items.Insert(index, tabItem);
                this.SelectedItem = tabItem;
            }
        }
        else if (index < parentTabControl.Items.IndexOf(tabItem))
        {
            parentTabControl.Items.Remove(tabItem);
            parentTabControl.Items.Insert(index, tabItem);
            parentTabControl.SelectedItem = tabItem;
        }
        else if (index > parentTabControl.Items.IndexOf(tabItem))
        {
            parentTabControl.Items.Remove(tabItem);
            parentTabControl.Items.Insert(index - 1, tabItem);
            parentTabControl.SelectedItem = tabItem;
        }
        e.Handled = true;
    }

    private void ResetState()
    {
        IsDraging = false;
        DragData = null;
    }
}

使用起来也很简单,只需要将TabControl 替换为 TabControlDragable 即可,事件已经绑定好了。

MainWindow.xaml

<Window x:Class="WpfLibraryTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfLibraryTest"
        xmlns:wpfLibrary="clr-namespace:WpfLibrary;assembly=WpfLibrary"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <wpfLibrary:TabControlDragable Grid.Row="0" x:Name="tabControl1">
            <TabItem Header="tabControl1Item1">
                <TextBlock>tabControl1Item1</TextBlock>
            </TabItem>
            <TabItem Header="tabControl1Item2">
                <TextBlock>tabControl1Item2</TextBlock>
            </TabItem>
            <TabItem Header="tabControl1Item3">
                <TextBlock>tabControl1Item3</TextBlock>
            </TabItem>
        </wpfLibrary:TabControlDragable>
        <wpfLibrary:TabControlDragable Grid.Row="1" x:Name="tabControl2">
            <TabItem Header="tabControl2Item1">
                <TextBlock>tabControl2Item1</TextBlock>
            </TabItem>
            <TabItem Header="tabControl2Item2">
                <TextBlock>tabControl2Item2</TextBlock>
            </TabItem>
            <TabItem Header="tabControl2Item3">
                <TextBlock>tabControl2Item3</TextBlock>
            </TabItem>
        </wpfLibrary:TabControlDragable>
    </Grid>
</Window>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中的 TreeView 控件本身不支持拖拽操作,但我们可以通过在 TreeView 上使用 PreviewMouseDown、MouseMove 和 PreviewMouseUp 事件来实现拖拽功能。 下面是一个简单的示例: ```xml <TreeView x:Name="myTreeView" PreviewMouseDown="MyTreeView_PreviewMouseDown" PreviewMouseMove="MyTreeView_PreviewMouseMove" PreviewMouseUp="MyTreeView_PreviewMouseUp"> <!-- TreeView 的内容 --> </TreeView> ``` 在代码中,我们需要定义一些变量来跟踪当前的拖拽操作: ```csharp private bool isDragging; private TreeViewItem draggedItem; private object draggedData; private bool isMouseDown; private Point clickPosition; ``` 然后,我们可以在 PreviewMouseDown 事件中记录鼠标单击位置和拖拽的数据: ```csharp private void MyTreeView_PreviewMouseDown(object sender, MouseButtonEventArgs e) { isMouseDown = true; clickPosition = e.GetPosition(myTreeView); var item = VisualUpwardSearch(e.OriginalSource as DependencyObject); if (item != null) { draggedItem = item; draggedData = draggedItem.DataContext; } } ``` 接下来,在 PreviewMouseMove 事件中检查是否开始了拖拽操作,如果是,则开始拖拽,并在 PreviewMouseUp 事件中结束拖拽: ```csharp private void MyTreeView_PreviewMouseMove(object sender, MouseEventArgs e) { if (isMouseDown && (Math.Abs(e.GetPosition(myTreeView).X - clickPosition.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(e.GetPosition(myTreeView).Y - clickPosition.Y) > SystemParameters.MinimumVerticalDragDistance)) { isDragging = true; DragDrop.DoDragDrop(draggedItem, draggedData, DragDropEffects.Move); } } private void MyTreeView_PreviewMouseUp(object sender, MouseButtonEventArgs e) { isMouseDown = false; if (isDragging) { isDragging = false; draggedItem = null; draggedData = null; } } ``` 最后,我们需要实现一个辅助方法,该方法可以从 TreeViewItem 的 VisualTree 中查找包含数据的项: ```csharp private TreeViewItem VisualUpwardSearch(DependencyObject source) { while (source != null && !(source is TreeViewItem)) { source = VisualTreeHelper.GetParent(source); } return source as TreeViewItem; } ``` 通过这些代码,我们可以实现 WPF 中的 TreeView 拖拽功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值