微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 从列表框拖放到文本框

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

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

我在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" VerticalAlignment="Top" Width="208" MouseDown="listBox1_MouseDown">
        <ListBoxItem Content="Gordon"/>
        <ListBoxItem Content="Nico"/>
    </ListBox>
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="405" Margin="289,0" textwrapping="Wrap" VerticalAlignment="Top" Width="364" SpellCheck.IsEnabled="True" Cursor="IBeam" AcceptsReturn="True" AllowDrop="True" dragenter="textBox1_dragenter"/>

</Grid>
</Window>

我在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;
    }

}
}

提前致谢.

解决方法

请尝试使用:

DragDrop.DoDragDrop( listBox1,lsitBox1.SelectedItem.ToString(),DragDropEffects.copy);

按照MSDN DragEnter event处理TextBox控件的dragenter事件

按照MSDN Drop event,处理TextBox控件的Drop事件

您还可以另外处理TextBox控件的DragOver事件,以进行更自定义的拖放处理和处理.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐