2009-11-17 92 views
0

我一直在從我的自定義控件派生的文本框,我遇到了我現在無法解決的問題。問題簡述:我的文本框包含純文本,其中包含我想要保持一致的標籤 - 到目前爲止,我已覆蓋文本選擇,因此它們只能作爲整個標籤進行選擇。 現在我已經轉移到處理拖動&下降。如果任何文本被放置在文本字段上並且被放在標籤上,我希望在標籤之前或之後移動插入。實際的問題是設置e.Handled = true。如果我將它設置爲true,它幾乎可以工作 - 文本通過我的例程插入,但不會從源代碼中刪除。如果將其設置爲false,則在執行我的方法之後,將運行原始文本框的插入方法。有沒有辦法改變事件路由?或者我從一開始就接近這個錯誤?WPF文本框派生的控件覆蓋拖放OnPreviewDrop(C銳)

代碼我的方法的: 保護覆蓋無效OnPreviewDragEnter(DragEventArgs E) { base.OnPreviewDragEnter(E); e.Handled = true; //讓我們得出我們自己的插入符... }

protected override void OnPreviewDrop(DragEventArgs e) 
    { 
     base.OnPreviewDrop(e); 

     fieldsReady = false; 
     int selStart = this.SelectionStart; 
     int selLength = this.SelectionLength; 

     string droppedData = (string)e.Data.GetData(DataFormats.StringFormat); 



     // where to insert 
     Point whereDropped = e.GetPosition(this); 
     int droppedIndex = GetCharacterIndexFromPoint(whereDropped, true); 
     if (droppedIndex == this.Text.Length - 1) 
     { 
      double c = GetRectFromCharacterIndex(droppedIndex).X; 
      if (whereDropped.X > c) 
       droppedIndex++; 
     } 


     // only if the source was us, do this: 
     if (this.SelectionLength > 0) // this means that we are dragging from our textbox! 
     { 

      // was there any selection? if so, remove it! 
      this.Text = this.Text.Substring(0, selStart) + this.Text.Substring(selStart + selLength); 
      e.Handled = true; 

      // 2DO!! alter the indices depending on the removed selection 

      // insertion 
      this.Text = this.Text.Substring(0, droppedIndex) + droppedData + this.Text.Substring(droppedIndex); 

     } 

    } 

回答

0

在OnPreviewDrop處理程序設置e.Effects可能產生的結果。我的經驗是的文字是從源碼中刪除。設置e.Effects = DragDropEffects.Copy單獨保留我的源文本,這是我期望的行爲。當設置e.Handled = true時,DragDropEffects.Move可能會幫助你。