如何在超链接中设置TabIndex?

时间:2013-07-03 08:38:23

标签: c# wpf tabindex

我是WPF的新手,我不知道该怎么做。 我尝试了以下代码 -

<TextBlock Grid.Column="3" Grid.Row="3" Visibility="{Binding Path=CanCreate, Converter={StaticResource BoolVisibilityConverter}}">
     <Hyperlink KeyboardNavigation.TabIndex="2" Command="{Binding Path=CreateCommand}">Create 
         <Hyperlink.Style>
             <Style TargetType="{x:Type Hyperlink}">
                 <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
                 <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=CanCreate, Converter={StaticResource BoolVisibilityConverter}}">
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
                    </DataTrigger>
                 </Style.Triggers>
             </Style>
         </Hyperlink.Style>
    </Hyperlink>
</TextBlock>

可见性取决于CanCreate Dependency属性。这里IsTabStop没有启用。有谁能提出解决方案。提前谢谢。

要求

我希望TabIndex在可见时设置为超链接。

1 个答案:

答案 0 :(得分:0)

DataTrigger您不需要将Hyperlink.Style更改为

<Hyperlink.Style>
      <Style TargetType="{x:Type Hyperlink}">
            <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
            <Style.Triggers>
                 <DataTrigger Binding="{Binding Path=CanCreate}" Value="True">
                      <Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
                 </DataTrigger>
             </Style.Triggers>
       </Style>
</Hyperlink.Style>

编辑:您可能还希望在TextBlockHyperlink不可见时删除焦点,您可以通过添加另一个DataTrigger来实现:

<Hyperlink.Style>
          <Style TargetType="{x:Type Hyperlink}">
                <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
                <Style.Triggers>
                     <DataTrigger Binding="{Binding Path=CanCreate}" Value="True">
                          <Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
                     </DataTrigger>
                     <DataTrigger Binding="{Binding Path=CanCreate}" Value="False">
                          <Setter Property="Focusable" Value="False" />
                     </DataTrigger>
                 </Style.Triggers>
           </Style>
    </Hyperlink.Style>