加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:51:00 所属栏目:百科 来源:网络整理
导读:我有一个非常简单的问题,但我似乎把这个想法弄复杂了,也许有人可以指导我或者帮助我一点. 我有一个列表框和文本框,我想将文本从列表框复制到文本框. 我在WPF中的代码如下: Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.co
我有一个非常简单的问题,但我似乎把这个想法弄复杂了,也许有人可以指导我或者帮助我一点.

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

我在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中拖放过程的更多信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读