[英]WPF/C#: Disable Drag & Drop for TextBoxes?
在 InitializeComponent() 之后使用以下内容
DataObject.AddCopyingHandler(textboxName, (sender, e) => { if (e.IsDragDrop) e.CancelCommand(); });
我个人创建了一个不允许拖动的自定义 TextBox 控件,如下所示:
/// <summary>
/// Represents a <see cref="TextBox"/> control that does not allow drag on its contents.
/// </summary>
public class NoDragTextBox:TextBox
{
/// <summary>
/// Initializes a new instance of the <see cref="NoDragTextBox"/> class.
/// </summary>
public NoDragTextBox()
{
DataObject.AddCopyingHandler(this, NoDragCopyingHandler);
}
private void NoDragCopyingHandler(object sender, DataObjectCopyingEventArgs e)
{
if (e.IsDragDrop)
{
e.CancelCommand();
}
}
}
使用 local:NoDragTextBox 而不是使用 TextBox,其中“local”是 NoDragTextBox 程序集位置的别名。 也可以扩展上述相同的逻辑以防止在 TextBox 上复制/粘贴。
有关更多信息,请查看http://jigneshon.blogspot.be/2013/10/c-wpf-snippet-disabling-dragging-from.html上对上述代码的引用
创建您的所有者用户控件 ex MyTextBox: TextBox 并覆盖:
protected override void OnDragEnter(DragEventArgs e)
{
e.Handled = true;
}
protected override void OnDrop(DragEventArgs e)
{
e.Handled = true;
}
protected override void OnDragOver(DragEventArgs e)
{
e.Handled = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.