SL4 文本框上的 IsTabStop = False

发布于 2024-11-03 10:03:24 字数 316 浏览 0 评论 0原文

我在文本框中将 IsTabStop 设置为 false,我知道这会使控件无法接收焦点,但根据 Silverlight Forums,它应该仍然能够接收鼠标事件。我在 tbxTotal_MouseLeftButtonUp 方法中连接了 MouseLeftButtonUp 事件和断点,并且在调试过程中永远不会命中它。 SL 论坛中的线程现在已经很旧了,所以也许这在某个更新中被更改了。我想要一个无法使用选项卡但仍可编辑的文本框。真的应该这么难吗?

I set IsTabStop to false on a text box and I know that this makes the control unable to receive focus, but according to the Silverlight Forums, it should still be able to receive mouse events. I have the MouseLeftButtonUp event wired and a breakpoint in my tbxTotal_MouseLeftButtonUp method, and it never gets hit during debugging. The thread in the SL Forums is pretty old now, so maybe this was changed in an update somewhere. I want a text box that can't be tabbed to, but is still editable. Should it really be this hard?

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

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

发布评论

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

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-11-10 10:03:24

我没有意识到这一点,但情况似乎如此,此外,我似乎无法让 MouseLeftButtonUp 触发。 MouseLeftButtonDown 确实会触发,并且使用它您可以进行此黑客攻击。

<TextBox IsTabStop="False" MouseLeftButtonDown="TextBox_MouseLeftButtonDown" />

然后在代码中你可以像这样处理事件。

    private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var textBox = ((TextBox) sender);
        textBox.IsTabStop = true;
        textBox.Focus();
        textBox.IsTabStop = false;
    }

将其包装在 CustomControl 中可能是值得的

public class FocusableTextBox : TextBox
{
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        if (!IsTabStop)
        {
            IsTabStop = true;
            Focus();
            IsTabStop = false;
        }

        base.OnMouseLeftButtonDown(e);
    }
}

I didn't realize this, but it seems to be the case, Additionally, I can't seem to get MouseLeftButtonUp to fire. MouseLeftButtonDown does fire though and using that you can do this hack.

<TextBox IsTabStop="False" MouseLeftButtonDown="TextBox_MouseLeftButtonDown" />

Then in code you can handle the event like this.

    private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var textBox = ((TextBox) sender);
        textBox.IsTabStop = true;
        textBox.Focus();
        textBox.IsTabStop = false;
    }

It might be worth while to wrap it in a CustomControl

public class FocusableTextBox : TextBox
{
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        if (!IsTabStop)
        {
            IsTabStop = true;
            Focus();
            IsTabStop = false;
        }

        base.OnMouseLeftButtonDown(e);
    }
}
狂之美人 2024-11-10 10:03:24

@seekerOfKnowledge:在 LostFocus 上禁用 IsTabStop 是一个很好的方法,但是您的重新聚焦黑客是不必要的。第一次没有任何明显的效果,因为 IsTabStop 的更改尚未生效。这种方法也可以与任何其他控制一起采用。

        var control = sender as Control;
        if (control != null)
        {
            control.MouseLeftButtonDown += (sender, args) =>
                {   //This event fires even if the control isn't allowed focus. 
                    //As long as the control is visible, it's typically hit-testable.
                    if (!control.IsTabStop)
                    {
                        control.IsTabStop = true;
                        //threading required so IsTabStop change can take effect before assigning focus
                        control.Dispatcher.BeginInvoke(() =>
                            {
                                control.Focus();
                            });
                    }
                };

            control.LostFocus += (sender, args) =>
                {   //Remove IsTabStop once the user exits the control
                    control.IsTabStop = false;
                };
        }

@seekerOfKnowledge: Disabling IsTabStop on the LostFocus is a good approach, but your re-focus hack is unnecessary. It fails to have any visible effect the first time around because the change of IsTabStop has not yet taken effect. This approach can be also be taken with any other control.

        var control = sender as Control;
        if (control != null)
        {
            control.MouseLeftButtonDown += (sender, args) =>
                {   //This event fires even if the control isn't allowed focus. 
                    //As long as the control is visible, it's typically hit-testable.
                    if (!control.IsTabStop)
                    {
                        control.IsTabStop = true;
                        //threading required so IsTabStop change can take effect before assigning focus
                        control.Dispatcher.BeginInvoke(() =>
                            {
                                control.Focus();
                            });
                    }
                };

            control.LostFocus += (sender, args) =>
                {   //Remove IsTabStop once the user exits the control
                    control.IsTabStop = false;
                };
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文