Reputation: 6397
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.
Upvotes: 6
Views: 11015
Reputation: 1524
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
Upvotes: 2
Reputation: 11
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;
}
Upvotes: 1
Reputation: 1507
Use the following after InitializeComponent()
DataObject.AddCopyingHandler(textboxName, (sender, e) => { if (e.IsDragDrop) e.CancelCommand(); });
Upvotes: 6
Reputation: 17133
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
Upvotes: 2