Silverlight RichTextBox 禁用鼠标选择

发布于 2024-08-30 09:11:22 字数 308 浏览 4 评论 0原文

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

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

关于如何解决这个问题有什么想法吗?谢谢。

I am writing a custom control based on RichTextBox that needs the ability to process MouseLeftButtonDown events but must not allow user-initiated selection (I'm doing everything programmatically).

I tried setting a flag in MouseLeftButtonDown to track dragging and then continuously setting the RichTextBox.Selection to nothing in the MouseMove event but the move event isn't fired until after I release the mouse button.

Any ideas on how to solve this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妳是的陽光 2024-09-06 09:11:22

这是我想出的解决方案:

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);
    }
}

Here's the solution I came up with:

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);
    }
}
瀞厅☆埖开 2024-09-06 09:11:22

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

Have you tried e.Handled = true in your event handler to see if that gets you the desired behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文