2009-07-15 98 views

回答

1

您可以輕鬆地包裹本文所述內容爲附加屬性/行爲......

即。 TextBoxManager.AllowDrag = 「假」(欲瞭解更多信息請查看以下2篇CodeProject上的文章 - Drag and Drop Sample和玻璃效果樣品link text

,或嘗試新的混合SDK的行爲

UPDATE

  • 另外請閱讀Bill Kempf的文章this關於附加行爲的文章
  • 而正如kek444在評論中指出的那樣,您只需在textbxo中創建一個默認樣式即可tached屬性集!
+1

當然檢查參考上面的代碼,在文本框默認樣式設置附加屬性,如果需要保存一些手工。 – 2009-07-15 12:05:15

3

使用以下後的InitializeComponent()

DataObject.AddCopyingHandler(textboxName, (sender, e) => { if (e.IsDragDrop) e.CancelCommand(); }); 
+1

對於任何想要這樣做的人來說,這是迄今爲止從代碼中完成的最簡單的方法。完整的解釋在這裏:http://www.switchonthecode.com/tutorials/wpf-snippet-disabling-dragging-from-a-textbox – stone 2011-03-02 22:22:06

0

創建擁有用戶的控制前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; 
    } 
+2

這是行不通的。它可以防止掉落,但不會從控件拖拽。此外,當您從其他地方的文本框中刪除文本時,可能會發生可怕的事情。 – Mark 2012-07-04 17:22:28

0

個人而言,我創建了一個自定義的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(); 
     } 
    } 
} 

而不是使用TextBox使用local:NoDragTextBox其中「local」是NoDragTextBox程序集位置的別名。同樣的上述邏輯也可以擴展,以防止在TextBox上覆制/粘貼。

欲瞭解更多信息,在http://jigneshon.blogspot.be/2013/10/c-wpf-snippet-disabling-dragging-from.html