将TreeViewItems拖放到TreeView的不可单击区域中[WPF] [英] Dragging and dropping TreeViewItems in non-clickable area of TreeView [WPF]

查看:81
本文介绍了将TreeViewItems拖放到TreeView的不可单击区域中[WPF]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在WPF中拖放树视图项的问题。

A question about dragging and dropping tree view items in WPF.

我最初的问题有点复杂。所以我简化了代码,这里是代码:
XAML

My original question is a little complex. So I simplified it, and here is the code: XAML

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
<Grid>
    <TreeView>
        <TreeView.ItemContainerStyle>
            <Style>
                <Setter Property="TreeViewItem.IsExpanded" Value="True"/>
                <Setter Property="TreeViewItem.Background" Value="LightBlue"/>
                <Setter Property="TreeViewItem.AllowDrop" Value="True"/>
                <EventSetter Event="TreeViewItem.MouseMove" Handler="TreeNode_MouseMove"/>
                <EventSetter Event="TreeViewItem.Drop" Handler="TreeNode_Drop"/>
            </Style>
        </TreeView.ItemContainerStyle>

        <TreeViewItem Header="first node in the tree"/>
        <TreeViewItem Header="second node in the tree"></TreeViewItem>
        <TreeViewItem Header="third node in the tree"></TreeViewItem>
    </TreeView>
</Grid>

和隐藏代码:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void TreeNode_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DragDrop.DoDragDrop(this, DateTime.Now.ToString(), DragDropEffects.Move);
        }
    }

    private void TreeNode_Drop(object sender, DragEventArgs e)
    {
        string str = (string)e.Data.GetData(typeof(string));
        MessageBox.Show(str);
    }
}

所以我想做的很简单,将一个树视图项目拖放到另一个树视图项目时,会弹出一个消息框。

So what I want to do is very simple, just pop up a message box when one tree view item is dragged and dropped on another tree view item.

当我将其正确放置在另一个项目上时,它可以正常工作,就像这样:

When I drop it right on another item, it works fine, like this:

但是如果我将其稍微拖出项目边界,它将无法正常工作,例如:

But if I drag it slightly off the boundary of the item, it does not work, like this:

鼠标光标显示为

所以现在我的问题是:如何使第二种条件起作用?

So now my question is: how to make the second condition work? How to make the Drop event still fire up when the dropping location is slightly off the boundary of the item?

预先感谢。

推荐答案

对TreeView本身使用AllowDrop属性和Drop事件。

use AllowDrop property and Drop event for TreeView itself.

这来自msdn: http://social.msdn.microsoft。 com / Forums / zh-CN / wpf / thread / 4b8bafe5-ae3e-439d-953a-f534a60dbb2d /

这篇关于将TreeViewItems拖放到TreeView的不可单击区域中[WPF]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆