0

我想在 C# 中将一个节点拖到Telerik RadTreeviw我的。我查看了这个解决方案,但它是用于并且它不起作用,因为这部分变得混乱TextboxwinformMicrosoft TreeviewTelerikRadTreeview

DoDragDrop(e.Node.Text, DragDropEffects.Copy);

有谁知道我的情况下的有效解决方案Telerik RadTreeView

4

2 回答 2

1

下面是如何将节点从 RadTreeView 拖到 RadTextBox (也可以用标准的 TextBox 替换,因为实际上 HostedTextBox 是标准的)

public partial class Form1 : Form
{
    Point clickedPoint;
    bool mouseDown = false;

    public Form1()
    {
        InitializeComponent();

        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.AllowDrop = true;
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragEnter += new DragEventHandler(HostedControl_DragEnter);
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragDrop += new DragEventHandler(HostedControl_DragDrop);

        radTreeView1.MouseDown += new MouseEventHandler(radTreeView1_MouseDown);
        radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_MouseMove);

    }

    void HostedControl_DragDrop(object sender, DragEventArgs e)
    {
        RadTreeNode node = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
        if (node != null)
        {
            radTextBox1.Text = node.Text;
        }
    }

    void HostedControl_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    void radTreeView1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseDown && IsRealDrag(e.Location, clickedPoint))
        {
            TreeNodeElement node = ((RadTreeView)sender).ElementTree.GetElementAtPoint(clickedPoint) as TreeNodeElement;
            if (node != null)
            {
                ((RadTreeView)sender).DoDragDrop(node.Data, DragDropEffects.Copy | DragDropEffects.Move);
            }
            mouseDown = false;
        }
    }

    void radTreeView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            mouseDown = true;
            clickedPoint = e.Location;
        }
    }

    private static bool IsRealDrag(Point mousePosition, Point initialMousePosition)
    {
        return (Math.Abs(mousePosition.X - initialMousePosition.X) >= SystemInformation.DragSize.Width) ||
            (Math.Abs(mousePosition.Y - initialMousePosition.Y) >= SystemInformation.DragSize.Height);
    }
}
于 2014-11-19T10:50:15.517 回答
0

我向@checho 添加了一些代码,这样我就可以在我的节点中拖放节点RadTreeView而无需设置其AllowDragDrop=true.

在节点之间拖动时,我在表单中添加了两个RadioButtons以更改Copy和模式。Cut

这是我的代码

public partial class Form1 : Form
{
    Point clickedPoint;
    bool mouseDown = false;

    public Form1()
    {
        InitializeComponent();

        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.AllowDrop = true;
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragEnter += new DragEventHandler(HostedControl_DragEnter);
        radTextBox1.TextBoxElement.TextBoxItem.HostedControl.DragDrop += new DragEventHandler(HostedControl_DragDrop);

        radTreeView1.MouseDown += new MouseEventHandler(radTreeView1_MouseDown);
        radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_MouseMove);
    radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_DragEnter);
    radTreeView1.MouseMove += new MouseEventHandler(radTreeView1_DragDrop);

    }

    void HostedControl_DragDrop(object sender, DragEventArgs e)
    {
        RadTreeNode node = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
        if (node != null)
        {
            radTextBox1.Text = node.Text;
        }
    }

    void HostedControl_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    void radTreeView1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseDown && IsRealDrag(e.Location, clickedPoint))
        {
            TreeNodeElement node = ((RadTreeView)sender).ElementTree.GetElementAtPoint(clickedPoint) as TreeNodeElement;
            if (node != null)
            {
                ((RadTreeView)sender).DoDragDrop(node.Data, DragDropEffects.Copy | DragDropEffects.Move);
            }
            mouseDown = false;
        }
    }

    void radTreeView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            mouseDown = true;
            clickedPoint = e.Location;
        }
    }

    private static bool IsRealDrag(Point mousePosition, Point initialMousePosition)
    {
        return (Math.Abs(mousePosition.X - initialMousePosition.X) >= SystemInformation.DragSize.Width) ||
            (Math.Abs(mousePosition.Y - initialMousePosition.Y) >= SystemInformation.DragSize.Height);
    }

    private void radTreeView1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = radioButtonCopy.Checked ? DragDropEffects.Copy : DragDropEffects.Move;
    }

    private void radTreeView1_DragDrop(object sender, DragEventArgs e)
    {

            RadTreeNode sourceNode = e.Data.GetData(typeof(RadTreeNode)) as RadTreeNode;
            // Retrieve the client coordinates of the drop location.
            Point targetPoint = rtvTree.PointToClient(new Point(e.X, e.Y));

            // Retrieve the node at the drop location.
            RadTreeNode targetNode = rtvTree.GetNodeAt(targetPoint);
            if (sourceNode != null && targetNode != null)
            {
                RadTreeNode clonedNode = (RadTreeNode) sourceNode.Clone();
                if (radioButtonCut.Checked)
                    sourceNode.Remove();
                targetNode.Nodes.Add(clonedNode);
                targetNode.Expanded = true;
            }
     }
}
于 2014-11-20T07:25:30.377 回答