【问题标题】:WPF TextBox: Cancelling drop operation leaves Insertion markerWPF TextBox:取消放置操作会留下插入标记
【发布时间】:2021-12-03 05:07:01
【问题描述】:

我有一个 WPF 应用程序,如果满足某些条件,我想取消对 TextBox 的拖放操作(请参阅下面的 shouldCancelDrop):

示例:

XAML:

   <TextBox PreviewDrop="TextBox_PreviewDrop" />

后面的代码:

    private void TextBox_PreviewDrop(object sender, DragEventArgs e)
    {
        //Decision if to cancel to drop
        var shouldCancelDrop = false;
        //if (...) shouldCancelDrop = true;
        
        if (shouldCancelDrop)
        {
            e.Handled = true;
            return;
        }

        //...
    }

问题是:离开 TextBox_PreviewDrop-Handler 与 e.Handled = true 后,“插入标记”总是留在文本框中。它看起来像一个文本光标,但它是浅灰色且不闪烁,请看这里(右边的黑色是文本光标):

.

我如何在取消放置时去掉这个插入标记?

它会一直留在 TextBox 中,直到我完成另一个拖动操作!

所以我正在寻找一些“CancelDropOperationAndRedrawTextBox”-Action...

【问题讨论】:

  • 当 Ctrl + V 键盘粘贴操作中止时,也会出现相同的图形故障。

标签: c# wpf drag-and-drop textbox


【解决方案1】:

您可以使用“PreviewDragOver”事件来禁止通过 DragDropEffects.None 放置元素:

private void TextBox_OnPreviewDragOver(object sender, DragEventArgs e)
{
  //Decision if to cancel to drop
  var shouldCancelDrop = false;
  //if (...) shouldCancelDrop = true;

  if (shouldCancelDrop)
  {
    e.Effects = DragDropEffects.None;
    return;
  }

  //...    
}

【讨论】:

  • 我确实将此标记为答案,因为我没有找到其他更好的解决方案来解决我的问题。我尝试像 textBox.InvalidateVisual() 或 textBox.UpdateLayout() 来摆脱这个 fuc**** 插入标记都没有成功。我的主要目的是显示一个工具提示,其中包含用户无法粘贴拖动内容的原因。我现在可以在“shouldCancelDrop”的“TextBox_OnPreviewDragOver”函数中执行此操作。这对我的情况来说已经足够了。谢谢@LuckyyStrike
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
  • 2011-12-19
  • 2017-12-23
相关资源
最近更新 更多