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

二次信任 提交于 2020-02-06 04:27:12

问题


If you look at how textboxes appear in Windows Explorer - if you rename a file it highlights the entire text. 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. I was wondering if there was a way in WPF to disable this functionality, to have it more like Windows Explorer?

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.


回答1:


You can use 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);
};


来源:https://stackoverflow.com/questions/37689282/wpf-how-to-disable-drag-and-drop-within-a-textbox-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!