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

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

如果您查看文本框在 Windows 资源管理器中的显示方式 - 如果您重命名文件,它会突出显示整个文本。但是,如果您拖动到 select 文本,它会更改 selection 以适应用户的拖动。

在 WPF 中,如果您 select 文本框的所有文本,然后将文本区域拖到 select 文本,它会尝试将文本拖放到文本框区域内。我想知道 WPF 中是否有一种方法可以禁用此功能,使其更像 Windows Explorer?

之所以需要它,主要是因为当人们重命名事物时,他们要么想重命名整个事物(激活重命名后退格,因为所有文本都突出显示),要么重命名其中的一部分(用户拖动以突出显示某些文本。)Windows Explorer 将两者结合起来并且效果很好,我需要在 WPF 中复制该功能。

您可以使用 DataObject.AddCopyingHandler :

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

编辑:

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

您可以在实际选择之前放弃当前选择:

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