2010-04-23 84 views
1

我正在編寫一個基於RichTextBox的自定義控件,它需要處理MouseLeftButtonDown事件的功能,但不允許用戶啓動選擇(我以編程方式執行所有操作)。Silverlight RichTextBox禁用鼠標選擇

我試圖MouseLeftButtonDown設置標誌來跟蹤拖動,然後不斷的RichTextBox.Selection設置爲沒有在MouseMove事件,但此舉事件不會觸發直到在我鬆開鼠標按鈕。

關於如何解決這個問題的任何想法?謝謝。

回答

2

這是我想出瞭解決方案:

public class CustomRichTextBox : RichTextBox 
{ 
    private bool _selecting; 

    public CustomRichTextBox() 
    { 
     this.MouseLeftButtonDown += (s, e) => 
     { 
      _selecting = true; 
     }; 
     this.MouseLeftButtonUp += (s, e) => 
     { 
      this.SelectNone(); 
      _selecting = false; 
     }; 
     this.KeyDown += (s, e) => 
     { 
      if (e.Key == Key.Shift) 
       _selecting = true; 
     }; 
     this.KeyUp += (s, e) => 
     { 
      if (e.Key == Key.Shift) 
       _selecting = false; 
     }; 
     this.SelectionChanged += (s, e) => 
     { 
      if (_selecting) 
       this.SelectNone(); 
     }; 
    } 

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) 
    { 
     base.OnMouseLeftButtonDown(e); 
     e.Handled = false; 
    } 

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) 
    { 
     base.OnMouseLeftButtonUp(e); 
     e.Handled = false; 
    } 

    public void SelectNone() 
    { 
     this.Selection.Select(this.ContentStart, this.ContentStart); 
    } 
} 
0

您是否在您的事件處理程序中嘗試過e.Handled = true以查看是否可以獲得所需的行爲。

+0

最初的解決方案,我想的作品,我的問題是,我不重寫RichTextBox.OnMouseLeftButtonUp()。我感謝您的幫助。 – David 2010-04-23 21:09:36