【问题标题】:Silverlight RichTextBox Disable Mouse SelectionSilverlight RichTextBox 禁用鼠标选择
【发布时间】:2010-04-24 06:37:49
【问题描述】:

我正在编写一个基于RichTextBox 的自定义控件,它需要能够处理MouseLeftButtonDown 事件但不能允许用户启动的选择(我正在以编程方式进行所有操作)。

我尝试在MouseLeftButtonDown 中设置一个标志来跟踪拖动,然后在MouseMove 事件中不断将RichTextBox.Selection 设置为空,但直到我释放鼠标按钮后才会触发移动事件。

关于如何解决这个问题的任何想法?谢谢。

【问题讨论】:

    标签: silverlight richtextbox


    【解决方案1】:

    这是我想出的解决方案:

    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】:

      您是否在您的事件处理程序中尝试过 e.Handled = true 以查看这是否可以让您获得所需的行为。

      【讨论】:

      • 我想的最初的解决方案是可行的,我的问题是我没有覆盖 RichTextBox.OnMouseLeftButtonUp()。感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多