如何防止使用 Tab 键切换到 UserControl?

发布于 2024-12-09 09:46:28 字数 237 浏览 1 评论 0原文

我有一个自定义弹出窗口覆盖了我的屏幕的一部分。当它打开时,我想禁用使用 Tab 键进入其后面的用户控件。我不想使用 IsEnabled 属性,因为我不想将所有控件灰显。

还有其他属性可以做同样的事情吗? IsTabStop 仅阻止选项卡在 UserControl 本身(而不是其子控件)上停止,并且 IsFocusable 不是 UserControl 的有效属性。

I have a custom Popup that overlays part of my screen. When it is open, I want to disable tabbing into the UserControl behind it. I do not want to use the IsEnabled property because I do not want to gray out all the controls.

Is there another property that does the same thing? IsTabStop only prevents the tab from stopping on the UserControl itself, not it's children, and IsFocusable isn't a valid property for a UserControl.

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

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

发布评论

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

评论(6

天荒地未老 2024-12-16 09:46:29

在我的情况下, KeyboardNavigation.TabNavigation="None" 的解决方案似乎会冒泡到所有其他父容器。在某些情况下这可能是不需要的。
所以我在后面的代码中想出了一个 PreviewKeyDown-Event ,如下所示:

    private void OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab)
        {
            ((Control)sender).Focus(); 
            e.Handled = true;
        }
    }

The solution with KeyboardNavigation.TabNavigation="None" seems to bubble up to all other parent containers in my case. This is maybe not wanted in some scenarios.
So I came up with a PreviewKeyDown-Event in code behind like this:

    private void OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab)
        {
            ((Control)sender).Focus(); 
            e.Handled = true;
        }
    }
-黛色若梦 2024-12-16 09:46:29

这对我有用:

// ...

var applicationWindow = Application.Current?.MainWindow;
if (applicationWindow != null)
{
    KeyboardNavigation.SetIsTabStop(
        element: applicationWindow, 
        isTabStop: false);

    KeyboardNavigation.SetTabNavigation(
        element: applicationWindow, 
        mode: false);
}

// ...

注意:如果只是暂时需要上述操作,最好有一个恢复键盘导航的备份计划。

This worked for me:

// ...

var applicationWindow = Application.Current?.MainWindow;
if (applicationWindow != null)
{
    KeyboardNavigation.SetIsTabStop(
        element: applicationWindow, 
        isTabStop: false);

    KeyboardNavigation.SetTabNavigation(
        element: applicationWindow, 
        mode: false);
}

// ...

Note: it is good to have a backup plan for restoring the Keyboard navigation in case if the above is needed only temporarily.

明天过后 2024-12-16 09:46:28

使用 KeyboardNavigation.TabNavigation 附加属性KeyboardNavigationMode.None

KeyboardNavigation.TabNavigation="None"

Use the KeyboardNavigation.TabNavigation Attached Property with KeyboardNavigationMode.None on your container control.

KeyboardNavigation.TabNavigation="None"
小姐丶请自重 2024-12-16 09:46:28

您可以将子控件上的 IsTabStop 绑定到 UserControl 上的 IsTabStop

这样,您只需设置一次。

You can bind IsTabStop on the child controls to IsTabStop on the UserControl.

That way, you only have to set it once.

时光匆匆的小流年 2024-12-16 09:46:28

您可以编写一个在顶部元素中设置的附加属性。
该附加属性将在所有子元素中递归地将 IsTabStop 设置为 false。

如果您需要任何帮助才能使其正常工作,请告诉我。

You could write an attached property that you would set in the top element.
That attached property would recursively set IsTabStop to false in all the child elements.

Let me know if you need any help getting this to work.

太阳哥哥 2024-12-16 09:46:28

只需将该属性绑定到用户控件即可。

    <UserControl x:Class="PDV.UserControls.InputField"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 // etc... then:                     
                 x:Name="Root" KeyboardNavigation.TabNavigation="Local" >

        <Grid>
           <TextBox Name="textBox"  
                TabIndex="{Binding Path=TabIndex, ElementName=Root}" 
                IsTabStop="{Binding Path=IsTabStop, ElementName=Root}"  />                
        </Grid>
    </UserControl>

Just bind that property to the user control.

    <UserControl x:Class="PDV.UserControls.InputField"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 // etc... then:                     
                 x:Name="Root" KeyboardNavigation.TabNavigation="Local" >

        <Grid>
           <TextBox Name="textBox"  
                TabIndex="{Binding Path=TabIndex, ElementName=Root}" 
                IsTabStop="{Binding Path=IsTabStop, ElementName=Root}"  />                
        </Grid>
    </UserControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文