简体   繁体   English

WPF-如何禁用文本框控件内的拖放?

[英]WPF - How to disable drag and drop WITHIN a textbox control?

If you look at how textboxes appear in Windows Explorer - if you rename a file it highlights the entire text. 如果您查看文本框在Windows资源管理器中的显示方式-如果您重命名文件,它将突出显示整个文本。 But if you drag to select text, it will change the selection to fit the user's drag. 但是,如果拖动以选择文本,它将更改选择以适合用户的拖动。

In WPF, if you select all the text of a textbox, then drag in the text area to select text, it will try and drag & drop the text within the textbox area. 在WPF中,如果选择一个文本框的所有文本,然后在文本区域中拖动以选择文本,它将尝试将文本拖放到文本框区域中。 I was wondering if there was a way in WPF to disable this functionality, to have it more like Windows Explorer? 我想知道WPF中是否有一种方法可以禁用此功能,使其更像Windows资源管理器?

It's needed mostly because when people rename things they either want to rename the whole thing (backspace after activating a rename, since all the text is highlighted) or part of it (user drags to highlight some text.) Windows Explorer combines both and it works very well, and I'd need to duplicate that functionality in WPF. 这是最需要的,因为人们重命名事物时,他们要么想重命名整个事物(激活重命名后的退格键,因为所有文本都被突出显示),要么要重命名一部分(用户拖动以突出显示一些文本。)Windows资源管理器将两者结合在一起并且可以工作很好,我需要在WPF中复制该功能。

You can use DataObject.AddCopyingHandler : 您可以使用DataObject.AddCopyingHandler

DataObject.AddCopyingHandler(textbox, (s, e) =>
{
    if (e.IsDragDrop) e.CancelCommand();
});

EDIT: 编辑:

when dragging to select text it doesn't set the cursor position as the beginning of the selection, it just uses the beginning of the text in the textbox 拖动以选择文本时,它不会将光标位置设置为所选内容的开头,而只是使用文本框中的文本开头

You can drop the current selection, right before actually selecting it : 您可以在实际选择之前删除当前选择:

textbox.PreviewMouseLeftButtonDown += (s, e) =>
{
    textbox.Select(0, 0);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM