2010-04-23 83 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