wpf 从列表框拖放到文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16583801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:48:26  来源:igfitidea点击:

Drag and Drop from listbox to textbox

c#wpfvisual-studio

提问by Silentdarkness

I have a very simple question i think but i seemed to have complicated the idea, perhaps someone can guide me or help me out a bit.

我有一个非常简单的问题,但我似乎把这个想法复杂化了,也许有人可以指导我或帮助我一下。

I have a listbox and textbox, i want to copy text from the list box to the textbox.

我有一个列表框和文本框,我想将文本从列表框复制到文本框。

My code in WPF is as follows:

我在WPF中的代码如下:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="465" Width="681">
<Grid>
    <ListBox x:Name="listbox1" HorizontalAlignment="Left" Height="405" Margin="10,10,0,0" VerticalAlignment="Top" Width="208" MouseDown="listbox1_MouseDown">
        <ListBoxItem Content="Gordon"/>
        <ListBoxItem Content="Nico"/>
    </ListBox>
    <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="405" Margin="289,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="364" SpellCheck.IsEnabled="True" Cursor="IBeam" AcceptsReturn="True" AllowDrop="True" DragEnter="textbox1_DragEnter"/>

</Grid>
</Window>

My code in C# is as follow and this is where I'm stuck:

我在 C# 中的代码如下,这就是我卡住的地方:

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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void listbox1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        DragDrop.DoDragDrop(listbox1, listbox1.SelectedItem.ToString(), DragDropEffects.Copy);
    }

    private void textbox1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effects = DragDropEffects.Copy;
    }

    private void textbox1_PreviewDragOver(object sender, DragEventArgs e)
    {
        e.Handled = true;
    }

}
}

Thanks in advance.

提前致谢。

回答by VS1

Please try to use:

请尝试使用:

DragDrop.DoDragDrop( listbox1,
                         lsitbox1.SelectedItem.ToString(),
                         DragDropEffects.Copy);

Handle DragEnterevent for the Textboxcontrol as per MSDN DragEnterevent

手柄DragEnter的事件Textbox控制,每MSDNDragEnter事件

Handle Dropevent for the Textboxcontrol as per MSDN Dropevent

手柄Drop的事件Textbox控制,每MSDNDrop事件

You can also additionally handle the DragOverevent for the Textboxcontrol for more customized Drag drop handling and processing.

您还可以额外处理控件的DragOver事件,以Textbox进行更多自定义的拖放处理和处理。

You can find more on Drag and Drop process in WPF in thisMSDN article.

您可以在这篇MSDN 文章中找到有关 WPF 中拖放过程的更多信息。

回答by Silentdarkness

Here is my c# answer to my Question, its working i just have one issue.

这是我对我的问题的 c# 答案,它的工作原理我只有一个问题。

The issue is that a list box item has to be selected in order to drag it, how do i select and drag the item at the same time on a mousedown event without having to click it first?

问题是必须选择列表框项目才能拖动它,如何在鼠标按下事件中同时选择和拖动项目而不必先单击它?

C#:

C#:

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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void listbox1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (listbox1.SelectedItems.Count > 0)
        {
            ListBoxItem mySelectedItem = listbox1.SelectedItem as ListBoxItem;
            if (mySelectedItem != null)
            {
                DragDrop.DoDragDrop(listbox1, mySelectedItem.Content.ToString(), DragDropEffects.Copy);
            }

        }
    }

    private void textbox1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effects = DragDropEffects.Copy;
    }

    private void textbox1_PreviewDragOver(object sender, DragEventArgs e)
    {
        e.Handled = true;
    }

    private void textbox1_Drop(object sender, DragEventArgs e)
    {
        string tstring;
        tstring = e.Data.GetData(DataFormats.StringFormat).ToString();
        textbox1.Text += " " + tstring;
    }

    private void textbox1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {

    }


}
}

回答by Nandha kumar

Do the following:

请执行下列操作:

private void listbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
                DragDrop.DoDragDrop(listbox1, listbox1.SelectedItem.ToString(), DragDropEffects.All);
        }