拖放文本复制

时间:2015-03-08 18:05:29

标签: c# wpf

我似乎在将按钮内容拖放到文本框中时遇到了一个特殊问题。如果我在调试中运行我的程序,它只会将按钮的内容附加到文本框一次,但是如果我在没有调试的情况下运行它,它会附加两次。

这是XAML:

<TextBox x:Name="tbxExpression" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Height="50" Margin="5,5,5,47.5" TextAlignment="Right" FontSize="24" Background="#F4F9FD" AllowDrop="True" DragEnter="tbxExpression_Dragging" Drop="tbxExpression_Drop" PreviewDragEnter="tbxExpression_Dragging" PreviewDragOver="tbxExpression_Dragging" DragOver="tbxExpression_Dragging"></TextBox>
<Button x:Name="btnNumOne" Grid.Row="2" Grid.Column="0" Content="1" Margin="5" FontSize="14" Background="#EBF1F6" PreviewMouseLeftButtonDown="button_PreviewMouseLeftButtonDown" MouseMove="button_PreviewMouseMove" PreviewMouseMove="button_PreviewMouseMove"></Button>

和CS:

private void tbxExpression_Dragging(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(string)))
            e.Effects = DragDropEffects.Copy;
        else
            e.Effects = DragDropEffects.None;

        e.Handled = true;
    }
    private void tbxExpression_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(string)))
        {
            // Get the dragged data and place it in textbox.
            string content = e.Data.GetData(typeof(string)).ToString();

            TextBox tbx = (TextBox)sender;
            tbx.AppendText(content);
        }
    }


    // Button Events for Dragging.
    private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startPoint = e.GetPosition(null); // Absolute position.
    }
    private void button_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (IsDragging(startPoint, e))
        {
            // Get the dragged button
            Button btn = (Button)sender;

            if (btn != null)
            {
                // Initialize the drag and drop.
                DataObject dragData = new DataObject(typeof(string), btn.Content.ToString());
                DragDrop.DoDragDrop(btn, dragData, DragDropEffects.Copy);
                e.Handled = true;
            }
        }
    }

    // Helper
    private static bool IsDragging(Point dragStart, MouseEventArgs e)
    {
        Vector diff = e.GetPosition(null) - dragStart;
        return e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance);
    }

如果我将btnNumOne的内容拖到文本框中并将其删除,我会得到&#34; 11&#34;在文本框中,但如果我调试并执行相同的操作,我只会得到&#34; 1&#34;喜欢它应该。有谁知道为什么我会得到这种重复的文字?

2 个答案:

答案 0 :(得分:1)

您可以通过添加以下行来检查它在Debug中附加文本的次数

           // Initialize the drag and drop.
                Debug.WriteLine("button_PreviewMouseMove");

这种情况发生了两次(你不能设置一个断点,否则你会松开阻力运动)。 您可能希望将Drop事件更改为PreviewDrop,覆盖tbx 的文本(这只是一种解决方法,以粗体查找有效的解决方案)并将该事件设置为Handled如下所示

private void tbxExpression_PreviewDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(string)))
    {
        // Get the dragged data and place it in textbox.
        string content = e.Data.GetData(typeof(string)).ToString();
        Debug.WriteLine("PreviewDrop");
        TextBox tbx = (TextBox)sender;
        tbx.Text = content;
        e.Handled=true;
    }
}

最后,如果您设置为Handled the button_PreviewMouseLeftButtonDown ,如下所示,它只会附加一次文本:-)

private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Debug.WriteLine("Down");
    startPoint = e.GetPosition(null); // Absolute position.
    e.Handled = true;
}
private void tbxExpression_PreviewDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(string)))
    {
        // Get the dragged data and place it in textbox.
        string content = e.Data.GetData(typeof(string)).ToString();
        Debug.WriteLine("PreviewDrop: " + String.Join(",",e.Data.GetFormats()));
        TextBox tbx = (TextBox)sender;
        tbx.AppendText(content);
        e.Handled=true;
    }
}

答案 1 :(得分:0)

请参阅以下代码。不知何故,它不接受字符串格式。我将其更改为按钮,它可以正常工作。

 Point startPoint;
    public MainWindow()
    {
        InitializeComponent();
    }
    private void tbxExpression_Dragging(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("Button"))
            e.Effects = DragDropEffects.Copy;
        else
            e.Effects = DragDropEffects.None;

        e.Handled = true;
    }
    private void tbxExpression_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("Button"))
        {
            // Get the dragged data and place it in textbox.
            var content = (Button)e.Data.GetData("Button");

            TextBox tbx = (TextBox)sender;
            tbx.Text = content.Content.ToString();
        }
    }


    // Button Events for Dragging.
    private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startPoint = e.GetPosition(null); // Absolute position.
    }
    private void button_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (IsDragging(startPoint, e))
        {
            // Get the dragged button
            Button btn = (Button)sender;

            if (btn != null)
            {
                // Initialize the drag and drop.
                DataObject dragData = new DataObject("Button", btn);
                DragDrop.DoDragDrop(btn, dragData, DragDropEffects.Copy);
                e.Handled = true;
            }
        }
    }

    // Helper
    private static bool IsDragging(Point dragStart, MouseEventArgs e)
    {
        Vector diff = e.GetPosition(null) - dragStart;
        return e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance);
    }