简体   繁体   English

如何为一组单选按钮启用箭头键键盘导航和禁用选项卡导航?

[英]How to enable arrow keys keyboard navigation and disable tab nagivation for a group of radio buttons?

I was trying to implement a dialog box with layout as something like this我试图用这样的布局实现一个对话框
在此处输入图像描述

I wanted keyboard behavior as我想要键盘行为

  1. Control at radio button 'a' OR 'b' OR 'c' -> Tab press -> Cancel button -> Tab press -> Save button -> Tab press -> Last selected radio button among 'a' OR 'b' or 'c' (even first radio button 'a' will do).控制单选按钮 'a' OR 'b' OR 'c' -> Tab press -> Cancel button -> Tab press -> Save button -> Tab press -> Last selected radio button among 'a' OR 'b' or 'c'(即使是第一个单选按钮'a'也可以)。
  2. Arrow key navigation between 'a', 'b' and 'c' radio buttons. “a”、“b”和“c”单选按钮之间的箭头键导航。

A very easy way is to use <RadioButtons> to group individual <Radiobutton> but that is not affordable with the overall project as using the dependency library winui 2+ is messing up with other parts of the code and affecting functionality of some feature.一种非常简单的方法是使用<RadioButtons>对单个<Radiobutton>进行分组,但这对于整个项目来说是负担不起的,因为使用依赖库 winui 2+ 会扰乱代码的其他部分并影响某些功能的功能。

After researching on inte.net and reading through this documentation, I tried <ListView> which was causing two issues在研究了 inte.net 并通读了这个文档之后,我尝试了<ListView> ,这导致了两个问题

  1. Pressing spacebar button no longer selects any option.按空格键按钮不再选择任何选项。
  2. Narrator always says "Selected" for each radio button even if the radio button is not selected.讲述人始终为每个单选按钮说“已选择”,即使未选择单选按钮也是如此。

Upon further search, I landed upon this .经过进一步搜索,我找到了这个.

Code:代码:

<ContentDialog>
   <StackPanel>
        <StackPanel XYFocusKeyboardNavigation="Enabled" TabFocusNavigation="Once">
            <RadioButton Content="a" XYFocusKeyboardNavigation="Enabled" />
            <RadioButton Content="b" XYFocusKeyboardNavigation="Enabled" />
            <RadioButton Content="c" XYFocusKeyboardNavigation="Enabled" />
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button x:Uid="Cancel" Name="CancelButton" Click="Cancel" Margin="5" IsEnabled="False"/>
            <Button x:Uid="Save" Name="SaveButton" Click="Save" Margin="5" IsEnabled="False"/>
        </StackPanel>
   </StackPanel>
</ContentDialog>

Upon implementation, most of the issues with narrator and navigation were gone, except for 1 issue:实施后,讲述人和导航的大部分问题都消失了,除了 1 个问题:
The control never lands on 'b' radio button.控件永远不会落在“b”单选按钮上。
Being on 'a' radio button, pressing down arrow key doesn't works.在“a”单选按钮上,按下箭头键不起作用。 Pressing up arrow key moves the control to 'c'.按向上箭头键将控件移动到“c”。
Being on 'c' radio button, pressing up arrow key doesn't works.在“c”单选按钮上,按向上箭头键不起作用。 Pressing down arrow key moves the control to 'a'.按下箭头键将控件移动到“a”。

I even tried XYFocusDownNavigationStrategy="Projection" on 'c' radio button and XYFocusUpNavigationStrategy="Projection" on 'a' radio button, but it didn't work.我什至在“c”单选按钮上尝试XYFocusDownNavigationStrategy="Projection" ,在“a”单选按钮上尝试了 XYFocusUpNavigationStrategy="Projection XYFocusUpNavigationStrategy="Projection" ,但它没有用。

How to fix this issue?如何解决这个问题?

The RadioButtons control from the WinUI 2 Library does exactly what you want.WinUI 2 库中的RadioButtons 控件完全可以满足您的需求。 When you press tab, you leave the radio buttons group and jump to the next element, for example the cancel button.当您按下 Tab 键时,您将离开单选按钮组并跳转到下一个元素,例如取消按钮。 When one RadioButton is focused, pressing arrow down/arrow up moves selection to the next RadioButton.当一个 RadioButton 获得焦点时,按向下箭头/向上箭头会将选择移动到下一个 RadioButton。

With the control, your code would look like this:使用控件,您的代码将如下所示:

<RadioButtons x:Name="BackgroundRadioButtons" Header="Options" 
    SelectionChanged="Options_SelectionChanged">
    <x:String>A</x:String>
    <x:String>B</x:String>
    <x:String>C</x:String>
</RadioButtons>

and in C#, you can handle the event:而在C#,你可以处理事件:

private void Options_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Handle event
}

For an interactive example of the control, you can also download the WinUI 2 Gallery .对于控件的交互式示例,您还可以下载WinUI 2 Gallery

I'd suggest that you could try the RadioButtons control in the WinUi 2.8 for UWP. It could meet all your requirement.我建议您可以试试 WinUi 2.8 中的RadioButtons控件 for UWP。它可以满足您的所有要求。

Here is the link about how to get started with WinUI2 in UWP: Getting started with the Windows UI 2 Library .以下是 UWP 中有关如何开始使用 WinUI2 的链接: Windows UI 2 库入门

It's simple to use like the following:使用起来很简单,如下所示:

 <StackPanel>
        <StackPanel >
            <muxc:RadioButtons  XYFocusKeyboardNavigation="Enabled"
                                SelectedIndex="0" SelectionChanged="RadioButtons_SelectionChanged" >
                <x:String>a</x:String>
                <x:String>b</x:String>
                <x:String>c</x:String>
            </muxc:RadioButtons>
          
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button x:Uid="Cancel" Name="CancelButton" Click="Cancel" Margin="5" IsEnabled="True"/>
            <Button x:Uid="Save" Name="SaveButton" Click="Save" Margin="5" IsEnabled="True"/>
        </StackPanel>
    </StackPanel>

When you run the code, the first item- a is selected by default.运行代码时,默认选择第一项 - a When you press Tab-> a gets focused -> press Tab -> Cancel button gets focused -> press Tab -> Save button gets focused -> press Tab -> a gets focused again.当您按 Tab-> a获得焦点时 -> 按 Tab -> 取消按钮获得焦点 -> 按 Tab -> 保存按钮获得焦点 -> 按 Tab -> a再次获得焦点。

And when a gets focused again, you could use arrow key to navigate to b or c .a再次聚焦时,您可以使用箭头键导航到bc It will never navigate to the buttons.它永远不会导航到按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM