且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按Tab键时检查RadioButton

更新时间:2023-12-03 08:32:46

您可以使用通过样式应用的数据触发器来设置IsChecked和您可以设置tabindex来控制您使用选项卡导航到哪个控件:

< Window.Resources> 
< Style TargetType =" RadioButton">
< Style.Triggers>
< DataTrigger Binding =" {Binding Path = IsFocused,RelativeSource = {RelativeSource Self}}"值= QUOT;真&QUOT;&GT;
< Setter Property =" IsChecked"值= QUOT;真&QUOT; /&GT;
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /Window.Resources>
< Grid>
< StackPanel>
< RadioButton KeyboardNavigation.TabIndex =" 0"> xxxx< / RadioButton>
< Button KeyboardNavigation.TabIndex =" 3">一些按钮< / Button>
< RadioButton KeyboardNavigation.TabIndex =" 1"> yyyy< / RadioButton>
< RadioButton KeyboardNavigation.TabIndex =" 2"> zzzzz< / RadioButton>
< / StackPanel>
< / Grid>
< / Window>

我的建议是在默认情况下更改Tab键顺序之前要三思而行。


It当您从默认订单更改时,往往会使用户感到困惑。


可能用户在此处特别要求的工作中有一些特定的逻辑。



I have a radio button group which has 3 radio buttons and 2 buttons. Currently if I press Tab then first radio button in the group is focused and on second Tab press it moves to button. If I press Up/Down arrows, focus is moved to other radio buttons in the group. 

The behavior I expect is, when I press a tab, the focus should move to other radio buttons in the group and the focused radio button should get selected leaving the other radiobuttons unchecked. Once the last radio button in the group is focused, next Tab press should focus on Buttons in the view. Could someone give a sample code which has this behavior. 

You can use a datatrigger applied via a style to set IsChecked and you can set tabindex to control which control you navigate to with a tab:

    <Window.Resources>
        <Style TargetType="RadioButton">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsFocused, RelativeSource={RelativeSource Self}}" Value="True">
                    <Setter Property="IsChecked" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <RadioButton KeyboardNavigation.TabIndex="0">xxxx</RadioButton>
            <Button      KeyboardNavigation.TabIndex="3">Some button</Button>
            <RadioButton KeyboardNavigation.TabIndex="1">yyyy</RadioButton>
            <RadioButton KeyboardNavigation.TabIndex="2">zzzzz</RadioButton>
        </StackPanel>
    </Grid>
</Window>

My advice would be to think twice before changing tab order from the default.

It tends to confuse users when you change from the default order.

Maybe there's some particular logic at work the users have specifically requested here though.