Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF/C#: Disable Drag & Drop for TextBoxes?

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.

like image 959
Joseph jun. Melettukunnel Avatar asked Jul 15 '09 11:07

Joseph jun. Melettukunnel


People also ask

Is WPF and C# same?

C# is a programming language. WPF is a technology used to develop rich GUI applications using C# (or any other . NET language).

Is WPF still relevant 2022?

“WPF would be dead in 2022 because Microsoft doesn't need to be promoting non-mobile and non-cloud technology. But WPF might be alive in that sense if it's the best solution for fulfilling specific customer needs today. Therefore, having a hefty desktop application needs to run on Windows 7 PCs with IE 8.

Is WPF used anymore?

Is WPF used anymore? Electron is also more progressive compared to UWP. However, some industries still rely on WPF concepts and tools like XAML MVVM to develop futuristic web, mobile, and desktop applications. None of Microsofts GUI frameworks ever really die.

Is WPF part of C#?

WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages. WPF was introduced as a part of . NET Framework 3.0 as the Windows library to build Windows client apps and the next generation of Windows Forms.


4 Answers

Use the following after InitializeComponent()

DataObject.AddCopyingHandler(textboxName, (sender, e) => { if (e.IsDragDrop) e.CancelCommand(); });
like image 153
Nitin Chaudhari Avatar answered Oct 14 '22 01:10

Nitin Chaudhari


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!
like image 36
rudigrobler Avatar answered Oct 14 '22 01:10

rudigrobler


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

like image 2
Ahmad Avatar answered Oct 13 '22 23:10

Ahmad


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;
    }
like image 1
Phuc Avatar answered Oct 14 '22 01:10

Phuc