【发布时间】:2009-07-15 11:54:52
【问题描述】:
有没有人知道如何为我的所有 TextBox 元素禁用拖放功能? 我找到了 here 的东西,但这需要我为所有元素运行一个循环。
【问题讨论】:
标签: c# wpf textbox drag-and-drop
有没有人知道如何为我的所有 TextBox 元素禁用拖放功能? 我找到了 here 的东西,但这需要我为所有元素运行一个循环。
【问题讨论】:
标签: c# wpf textbox drag-and-drop
在 InitializeComponent() 之后使用以下内容
DataObject.AddCopyingHandler(textboxName, (sender, e) => { if (e.IsDragDrop) e.CancelCommand(); });
【讨论】:
您可以轻松地将本文描述的内容包装到附加的属性/行为中......
即。 TextBoxManager.AllowDrag="False"(有关更多信息,请查看这两篇 CodeProject 文章 - Drag and Drop Sample 和 Glass Effect Samplelink text)
或者尝试新的 Blend SDK 的行为
更新
【讨论】:
创建您的所有者用户控件 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;
}
【讨论】:
我个人创建了一个不允许拖动的自定义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上述代码的参考
【讨论】: