动态阻止 TextBox 获得焦点

发布于 2024-12-08 13:40:12 字数 1646 浏览 0 评论 0原文

我有一个 System.Windows.Controls.TextBox ,我希望其行为如下:当您单击它时,会动态确定 TextBox 是否获得焦点。这是一个玩具应用程序,其中包含完成此任务的失败尝试:

<!-- MainWindow.xaml -->
<Window x:Class="Focus.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Label Name="myLabel" Grid.Row="0" Background="Red"></Label>
        <TextBox Name="myTextbox" Grid.Row="1" Background="Green"></TextBox>
    </Grid>
</Window>

// MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace Focus
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            myTextbox.PreviewGotKeyboardFocus += myTextbox_GotKeyboardFocus;
        }

        private static readonly Random myRandom = new Random();

        private void myTextbox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            int randomInt = myRandom.Next(0, 2); // 0 or 1
            myLabel.Content = randomInt;
            if(randomInt==0)
            {
                // PREVENT FOCUS - INSERT CODE HERE. (The line below is a failed attempt.)
                FocusManager.SetFocusedElement(this, myLabel);
            }
        }
    }
}

I have a System.Windows.Controls.TextBox which I would like to behave as follows: When you click it, it is determined dynamically if the TextBox gets focus or not. Here's a toy application which contains a failed attempt at accomplishing this:

<!-- MainWindow.xaml -->
<Window x:Class="Focus.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Label Name="myLabel" Grid.Row="0" Background="Red"></Label>
        <TextBox Name="myTextbox" Grid.Row="1" Background="Green"></TextBox>
    </Grid>
</Window>

// MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace Focus
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            myTextbox.PreviewGotKeyboardFocus += myTextbox_GotKeyboardFocus;
        }

        private static readonly Random myRandom = new Random();

        private void myTextbox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            int randomInt = myRandom.Next(0, 2); // 0 or 1
            myLabel.Content = randomInt;
            if(randomInt==0)
            {
                // PREVENT FOCUS - INSERT CODE HERE. (The line below is a failed attempt.)
                FocusManager.SetFocusedElement(this, myLabel);
            }
        }
    }
}

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

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

发布评论

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

评论(2

傻比既视感 2024-12-15 13:40:12

下面的代码似乎可以解决问题:

// PREVENT FOCUS - INSERT CODE HERE.
myLabel.Focusable = true;
myLabel.Focus();
myLabel.Focusable = false;

我还将这行代码:更改

myTextbox.PreviewGotKeyboardFocus += myTextbox_GotKeyboardFocus;

为:

myTextbox.GotFocus += myTextbox_GotKeyboardFocus;

The following code seems to do the trick:

// PREVENT FOCUS - INSERT CODE HERE.
myLabel.Focusable = true;
myLabel.Focus();
myLabel.Focusable = false;

I also changed this line of code:

myTextbox.PreviewGotKeyboardFocus += myTextbox_GotKeyboardFocus;

into this:

myTextbox.GotFocus += myTextbox_GotKeyboardFocus;
梦初启 2024-12-15 13:40:12

我希望您知道您已连接到键盘焦点(TAB 键)。

void textBox1_GotFocus(object sender, System.EventArgs e)
{
    if (!this.checkBox1.Checked)
        this.checkBox1.Focus();

    else
        this.textBox1.Focus();

}

I hope you know that you hooked up to the keyboard focus (TAB key).

void textBox1_GotFocus(object sender, System.EventArgs e)
{
    if (!this.checkBox1.Checked)
        this.checkBox1.Focus();

    else
        this.textBox1.Focus();

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