代码之家  ›  专栏  ›  技术社区  ›  David

Silverlight RichTextBox禁用鼠标选择

  •  1
  • David  · 技术社区  · 15 年前

    我正在写一个基于 RichTextBox 这需要处理的能力 MouseLeftButtonDown 事件,但不能允许用户启动的选择(我做的一切编程)。

    我试着把旗子插进去 跟踪拖动,然后连续设置 RichTextBox.Selection MouseMove

    有什么办法解决这个问题吗?谢谢。

    2 回复  |  直到 15 年前
        1
  •  2
  •   David    15 年前

    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);
        }
    }
    
        2
  •  0
  •   AnthonyWJones    15 年前

    你试过了吗 e.Handled = true 在您的事件处理程序中,查看这是否会使您获得所需的行为。