问题
Does anyone have an idea how I can disable Drag & Drop for all my TextBox Elements? I found something here, but that would need me to run a loop for all Elements.
回答1:
You can easily wrap what this article describes into a attached property/behaviours...
ie. TextBoxManager.AllowDrag="False" (For more information check out these 2 CodeProject articles - Drag and Drop Sample and Glass Effect Samplelink text)
Or try out the new Blend SDK's Behaviors
UPDATE
- Also read this article by Bill Kempf about attached behaviors
- And as kek444 pointed out in the comments, you then just create a default style for textbxo whit this attached property set!
回答2:
Use the following after InitializeComponent()
DataObject.AddCopyingHandler(textboxName, (sender, e) => { if (e.IsDragDrop) e.CancelCommand(); });
回答3:
Create your owner user control ex MyTextBox: TextBox and override:
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;
}
回答4:
Personally I created a custom TextBox control that does not allow drag as follows:
/// <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();
}
}
}
Instead of using TextBox use local:NoDragTextBox where "local" being the alias to the location of the NoDragTextBox assembly. The same above logic also can be extended to prevent Copy/Paste on TextBox .
For more info check the reference to the above code at http://jigneshon.blogspot.be/2013/10/c-wpf-snippet-disabling-dragging-from.html
来源:https://stackoverflow.com/questions/1130949/wpf-c-disable-drag-drop-for-textboxes