如何在WPF中禁用隐藏元素的tabindex [英] How to disable tabindex for hidden element in WPF

查看:213
本文介绍了如何在WPF中禁用隐藏元素的tabindex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WPF表格并说我有一个文本框,按钮X,按钮Y和几个超链接。按钮x和文本框最初是可见的,按钮Y,隐藏超链接(当用户单击按钮X时将显示)。



当我按Tab键时,焦点会隐藏超链接,虽然它不会让它们可见,但仍然会显示一种占位符,它看起来很糟糕。



所以,基本上,我想要禁用隐藏元素的tabindexing并启用可见元素



以下是我的XAML文件的一部分...



< pre>< Window x:Class =WPF_TabIndexDemo.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d =http://schemas.microsoft.com/expression/blend/2008
xmlns:mc =http://schemas.openxmlformats.org/markup-compatibility/2006
xmlns:local =clr-namespace:WPF_TabIndexDemo
mc:Ignorable =d
Title =MainWindowHeight = 350宽度=525>
< Border BorderThickness =0BorderBrush =#FF999999Margin =1,1,1,1>
< Border.Effect>
< DropShadowEffect Color =#ccccccOpacity =0.83Direction =60BlurRadius =50/>
< /Border.Effect>
<网格背景=白色>
< Grid.RowDefinitions>
< RowDefinition Height =0 */>
< RowDefinition />
< /Grid.RowDefinitions>

<标签x:名称=标签内容=JioCloudGrid.RowSpan =2VerticalAlignment =TopVerticalContentAlignment =CenterMargin = - 1,-1, - 1,0Height =26Padding =30,0,0,0FontFamily =/ JioCloud; component / Views / Fonts /#JioType-LightFontSize =13FontWeight =Regular>
< Label.Style>
< Style TargetType ={x:Type Label}>
< Setter Property =Background>
< Setter.Value>
< LinearGradientBrush StartPoint =0,0EndPoint =0,1>
< GradientStop Color =#f4f4f4Offset =0/>
< GradientStop Color =#d4d1d4Offset =1/>
< / LinearGradientBrush>
< /Setter.Value>
< / Setter>
< / Style>
< /Label.Style>
< / Label>
< Image x:Name =ImageHorizo​​ntalAlignment =LeftHeight =16Margin =8,4,0,0Grid.Row =0Grid.RowSpan =2VerticalAlignment =TopWidth =16RenderTransformOrigin =0.5,-3.286/>
< Button x:Name =BtnCloseCommand ={Binding CloseWindowCommand}Width =24Height =24Margin =606,-1,-2,416
Grid.Row =0Grid.RowSpan =2>
< Image Width =12Height =12Margin =4,6,4,2/>
< / Button>
< Button x:Name =BtnMinimizeCommand ={Binding MinimizeWindowCommand}Height =24Width =24Margin =584,2,20,413
Grid.Row = 0Grid.RowSpan =2>
< Image Width =12Height =12Margin =6,9,6,3/>
< / Button>

< Grid Margin =141,122,136,50Grid.Row =0Grid.RowSpan =2Background =White>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =0 */>
< ColumnDefinition />
< /Grid.ColumnDefinitions>
< TextBox Horizo​​ntalAlignment =Leftx:Name =TbEmailAddressVerticalContentAlignment =CenterVerticalAlignment =CenterWidth =350BorderThickness =0,0,0,2Margin =0, 79,0,162TextOptions.TextHintingMode =AnimatedFontFamily =/ JioCloud; component / Views / Fonts /#JioType-BoldFontSize =15Grid.ColumnSpan =2Foreground =BlackBackground =White高度=25BorderBrush =#FF9B9B9B>
< TextBox.Style>
< Style TargetType ={x:Type TextBox}>
< Setter Property =BorderBrushValue =#c41215/>
< Style.Triggers>
< DataTrigger Binding ={Binding ElementName = LbEmailError,Path = Text}Value =>
< Setter Property =BorderBrushValue =#7d7d7d/>
< / DataTrigger>
< DataTrigger Binding ={Binding ElementName = LbEmailError,Path = Text}Value ={x:Null}>
< Setter Property =BorderBrushValue =#7d7d7d/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /TextBox.Style>
< / TextBox>
< TextBlock x:Name =TbForgotPasswordBackground =WhiteForeground =#FF9B9B9BHorizo​​ntalAlignment =LeftHeight =21Margin =254,81,-18,164VerticalAlignment =Center Width =115Grid.ColumnSpan =2
Grid.Column =0Visibility =Hidden>
< Hyperlink Foreground =#FF9B9B9BBackground =WhiteFontSize =13FontFamily =/ JioCloud; component / Views / Fonts /#JioType-LightNavigateUri =https://www.jio .com / Jio / portal / forgotPasswordTextDecorations =None>
<运行文字=忘记密码/>
< / Hyperlink>
< / TextBlock>

< TextBlock x:Name =TbResendOtpBackground =WhiteForeground =#FF9B9B9BHorizo​​ntalAlignment =LeftHeight =21Margin =280,80,-17,165VerticalAlignment =CenterWidth =88Grid.ColumnSpan =2
Grid.Column =0Visibility =Hidden>
< Hyperlink Foreground =#FF9B9B9BBackground =WhiteFontSize =13FontFamily =/ JioCloud; component / Views / Fonts /#JioType-LightNavigateUri =https://www.jio .com / Jio / portal / forgotPasswordTextDecorations =None>
< Run Text =Resend OTP/>
< LineBreak>< / LineBreak>
< / Hyperlink>
< / TextBlock>


< TextBlock x:Name =TbGoBackForeground =#FF9B9B9BHorizo​​ntalAlignment =CenterHeight =28Margin =132,199,154,39VerticalAlignment =Center Width =65Grid.ColumnSpan =2
Grid.Column =0Visibility =HiddenRenderTransformOrigin =0.658,0.571KeyboardNavigation.TabNavigation =None>
< Hyperlink Foreground =#FF9B9B9BFontSize =14FontWeight =ExtraBoldFontFamily =/ JioCloud; component / Views / Fonts /#JioType-BoldNavigateUri =https://www.jio .com / Jio / portal / forgotPassword>
< Run Text =Go Back/>
< / Hyperlink>
< / TextBlock>
< / Grid>


<按钮x:名称=BtnLoginCursor =HandBorderThickness =50Horizo​​ntalAlignment =CenterContent =LOGINFontFamily =/ JioCloud; component /视图/字体/ #JioType-LightVerticalAlignment =CenterWidth =128Height =35Margin =240,264,260,139
Grid.Row =0Grid.RowSpan =2IsDefault = trueFontSize =15Foreground =#FFF9F3F3Visibility =Hidden>
< / Button>
< Button x:Name =BtnContinueCursor =HandBorderThickness =50Horizo​​ntalAlignment =CenterContent =ContinueFontFamily =/ JioCloud; component / Views / Fonts / #JioType-Light VerticalAlignment =CenterWidth =128Height =35Margin =240,272,260,131
Grid.Row =0Grid.RowSpan =2IsDefault =trueFontSize =15Foreground =#FFF9F3F3Click =BtnContinue_Click>
< / Button>
< TextBlock Grid.Row =0Height =25Grid.RowSpan =2x:Name =PolicyLabelMargin =147,414,115,-1Foreground =Gray>
< TextBlock Width =305Height =17Padding =2FontSize =9FontFamily =/ JioCloud; component / Views / Fonts /#JioType-LightHorizo​​ntalAlignment =CenterVerticalAlignment =中心RenderTransformOrigin =0.544,0.294>继续登录,即表示您同意
< Hyperlink x:Name =TermsOfServiceHyperlinkFontFamily =/ JioCloud; component / Views / Fonts /#JioType- LightFontSize =9NavigateUri =https://static.jiodrive.com/tos.html>服务条款
< Hyperlink.Style>
< Style>
< Setter Property =Hyperlink.ForegroundValue =DarkOrange>< / Setter>
< Style.Triggers>
< Trigger Property =Hyperlink.IsMouseOverValue =True>
< Setter Property =Hyperlink.ForegroundValue =#F37F20>< / Setter>
< / Trigger>
< /Style.Triggers>
< / Style>
< /Hyperlink.Style>< / Hyperlink>
< TextBlock Padding =2FontSize =9FontFamily =/ JioCloud; component / Views / Fonts /#JioType-LightText =& VerticalAlignment = 顶/>
< Hyperlink x:Name =PrivacyPolicyHyperlinkFontFamily =/ JioCloud; component / Views / Fonts /#JioType-LightFontSize =9NavigateUri =https://static.jiodrive.com/privacypolicy .html>隐私政策
< Hyperlink.Style>
< Style>
< Setter Property =Hyperlink.ForegroundValue =DarkOrange>< / Setter>
< Style.Triggers>
< Trigger Property =Hyperlink.IsMouseOverValue =True>
< Setter Property =Hyperlink.ForegroundValue =#F37F20>< / Setter>
< / Trigger>
< /Style.Triggers>
< / Style>
< /Hyperlink.Style>< / Hyperlink>
< / TextBlock>
< / TextBlock>
< / Grid>
< / Border>
< / Window>





我尝试过:



1.我试图设置

 Focusable =False

但它不起作用。



2.设置

 KeyboardNavigation.TabNavigation =无

它有效... 但是,这是静态变化。即当我显示这些超链接时,无法对它们进行tabindex。这是因为我无法动态设置 KeyboardNavigation.TabNavigation = none

如何设置 KeyboardNavigation.TabNavigation =无隐藏仅限元素。



3.尝试使用TabStop,但它可用于表单而不适用于Windows

解决方案

< blockquote>我不确定你为什么要那样做。切换可见性可启用/禁用tabstops。您可以使用以下方法进行检查:

 <  窗口  

x:Class = Tabbing.MainWindow

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http: //schemas.microsoft.com/winfx/2006/xaml\"

mc:可忽略 = d

xmlns:d = http:// schemas.microsoft.com/expression/blend/2008\"

xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006

< span class =code-attribute> 标题 = MainWindow WindowStartupLocation = CenterScreen

高度 = 350 < span class =code-attribute>宽度 = 525 >
< Window.Resources < span class =code-keyword>>
< Storyboard x:Key = OnChecked >
< ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = (UIElement.Visibility) Storyboard.TargetName = MyButton >
< DiscreteObjectKeyFrame KeyTime = 0 = {x:静态Visibility.Visible} / >
< / ObjectAnimationUsingKeyFrames >
< ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = (ContentControl.Content) Storyboard.TargetName = toggleButton >
< DiscreteObjectKeyFrame KeyTime = 0 = 显示 / >
< / ObjectAnimationUsingKeyFrames >
< / Storyboard >
< 故事板 x:键 = OnUnchecked >
< ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = (UIElement.Visibility) Storyboard.TargetName = MyButton >
< DiscreteObjectKeyFrame KeyTime = 0 = {x:Static Visibility.Hidden} / >
< / ObjectAnimationUsingKeyFrames >
< ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = (ContentControl.Content) Storyboard.TargetName = toggleButton >
< DiscreteObjectKeyFrame KeyTime = 0 = 显示< /跨度> <跨度class =code-keyword> / >
< < span class =code-leadattribute> / ObjectAnimationUsingKeyFrames >
< / Storyboard >
< / Window.Resources >
< Window.Triggers >
< EventTrigger RoutedEvent = ToggleButton.Checked SourceName = toggleButton > ;
< BeginStoryboard 故事板 = {StaticResource OnChecked} / >
< ; / EventTrigger >
< EventTrigger RoutedEvent = ToggleButton.Unchecked < span class =code-attribute>来源姓名 = toggleButton >
< BeginStoryboard 故事板 = {StaticResource OnUnchecked} / >
< / EventTrigger >
< ; / Window.Triggers >

< 网格 >
< Grid.RowDefinitions >
< RowDefinition / >
< RowDefinition / >
< RowDefinition / >
< RowDefinition / >
< RowDefinition / >
< / Grid.RowDefinitions >
< Grid.Resources >
< 样式 TargetType = {x:Type TextBox} >
< Setter Property = Horizo​​ntalAlignment = 中心 / > ;
< Setter 属性 = VerticalAlignment = 中心 / >
< / Style >
< span class =code-keyword>< 样式 TargetType = {x:输入StackPanel} >
< Setter Property = Horizo​​ntalAlignment = 中心 / >
< Setter 属性 = VerticalAlignment = 中心 / >
< span class =code-keyword>< / Style >
< 样式 TargetType = {x:Type ToggleButton} >
< Setter < span class =code-attribute> 属性 = 填充 = 10 5 / >
< Setter 属性 = 保证金 = 10 / < span class =code-keyword>>
< / Style >
< 样式 TargetType = {x:类型按钮} >
< Setter 属性 = 填充 = 10 5 / >
< Setter Property = 保证金 = 10 / >
< / Style >
< / Grid.Resources >

< TextBox 宽度 = 200 / >
<TextBox Width=\"200\" Grid.Row=\"1\"/>
<TextBox Width=\"200\" Grid.Row=\"2\"/>
<TextBox Width=\"200\" Grid.Row=\"3\"/>

<StackPanel Orientation=\"Horizontal\" Grid.Row=\"4\">
<ToggleButton x:Name=\"toggleButton\" Content=\"Hide\" IsChecked=\"True\"/>
<Button x:Name=\"MyButton\" Content=\"HELLO!\" Visibility=\"Visible\"/>
<< span class=\"code-leadattribute\">/StackPanel>
</Grid>
</Window>


Your info was very helpful and solved my issue. I had a TextBlock around a Hyperlink control. I was able to add data binding for Visibility and KeyboardNavigation.TabNavigation in the text control and it works great.



Add to xaml resource at top of page.

<!-- Visibility converters for MVVM values --> 
<Helpers:BoolToVisibilityConverter x:Key=\"BoolToVisibility\"/>
<Helpers:BoolToTabNavigationConverter x:Key=\"BoolToTabNavigation\"/>



Use converters in TextBlock

<TextBlock Visibility=\"{Binding IsLinkVisible, Converter={StaticResource BoolToVisibility}}\" KeyboardNavigation.TabNavigation=\"{Binding IsLinkVisible, Converter={StaticResource BoolToTabNavigation}}\"> 
\t<Hyperlink NavigateUri=\"{Binding url1}\">
\t\t<TextBlock Text=\"{Binding link1}\"/>
\t</Hyperlink>
</TextBlock>



Covert bool to KeyboardNavigationMode

// bool to KeyboardNavigationMode converter for MVVM classes 
[ValueConversion(typeof(bool), typeof(KeyboardNavigationMode))]
public class BoolToTabNavigationConverter : IValueConverter
{
\tenum Parameters
\t{
\t\tNormal, Inverted
\t}
\tpublic object Convert(object value, Type targetType, object parameter, CultureInfo culture)
\t{
\t\tbool tabStop = (bool)value;
\t\tvar direction = Parameters.Normal;
\t\tif (parameter != null)
\t\t\tdirection = (Parameters)Enum.Parse(typeof(Parameters), (string)parameter);
\t\tif (direction == Parameters.Inverted)
\t\t\ttabStop = !tabStop;
\t\treturn tabStop ? KeyboardNavigationMode.Local : KeyboardNavigationMode.None;
\t}

\tpublic object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
\t{
\t\treturn null;
}
}


I have WPF form and say I have a textbox, button X, button Y and couple of hyperlinks. Button x and textbox are initially visible and button Y, hyperlinks are hidden(will be visible when user clicks button X).

When I press tab, focus goes over hidden hyperlink, it won't make them visible though, but still shows a focus on a kind of place holder, which looks bad.

So, basically, I want to disable tabindexing for hidden elements and enable for visible elements

Below is part of my XAML file...

<pre><Window x:Class="WPF_TabIndexDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_TabIndexDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Border BorderThickness="0" BorderBrush="#FF999999" Margin="1,1,1,1">
        <Border.Effect>
            <DropShadowEffect Color="#cccccc" Opacity="0.83" Direction="60" BlurRadius="50"/>
        </Border.Effect>
        <Grid Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="0*"/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <Label x:Name="Label" Content="JioCloud" Grid.RowSpan="2" VerticalAlignment="Top" VerticalContentAlignment="Center" Margin="-1,-1,-1,0" Height="26" Padding="30,0,0,0" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" FontSize="13" FontWeight="Regular">
                <Label.Style>
                    <Style TargetType="{x:Type Label}">
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="#f4f4f4" Offset="0"/>
                                    <GradientStop Color="#d4d1d4" Offset="1"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Label.Style>
            </Label>
            <Image x:Name="Image" HorizontalAlignment="Left" Height="16" Margin="8,4,0,0" Grid.Row="0" Grid.RowSpan="2" VerticalAlignment="Top" Width="16" RenderTransformOrigin="0.5,-3.286" />
            <Button x:Name="BtnClose"  Command="{Binding CloseWindowCommand}" Width="24" Height="24" Margin="606,-1,-2,416"
                    Grid.Row="0" Grid.RowSpan="2">
                <Image Width="12" Height="12"  Margin="4,6,4,2" />
            </Button>
            <Button x:Name="BtnMinimize"  Command="{Binding MinimizeWindowCommand}" Height="24" Width="24" Margin="584,2,20,413"
                    Grid.Row="0" Grid.RowSpan="2">
                <Image Width="12" Height="12"  Margin="6,9,6,3" />
            </Button>

            <Grid Margin="141,122,136,50" Grid.Row="0" Grid.RowSpan="2" Background="White" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0*"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBox HorizontalAlignment="Left" x:Name="TbEmailAddress"  VerticalContentAlignment="Center" VerticalAlignment="Center" Width="350" BorderThickness="0,0,0,2" Margin="0,79,0,162" TextOptions.TextHintingMode="Animated" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Bold" FontSize="15" Grid.ColumnSpan="2" Foreground="Black" Background="White" Height="25" BorderBrush="#FF9B9B9B" >
                    <TextBox.Style>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="BorderBrush" Value="#c41215"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=LbEmailError, Path=Text}" Value="">
                                    <Setter Property="BorderBrush" Value="#7d7d7d" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=LbEmailError, Path=Text}" Value="{x:Null}">
                                    <Setter Property="BorderBrush" Value="#7d7d7d" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <TextBlock x:Name="TbForgotPassword" Background="White" Foreground="#FF9B9B9B" HorizontalAlignment="Left" Height="21" Margin="254,81,-18,164" VerticalAlignment="Center" Width="115" Grid.ColumnSpan="2"
                           Grid.Column="0" Visibility="Hidden">
                    <Hyperlink Foreground="#FF9B9B9B" Background="White" FontSize="13" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" NavigateUri="https://www.jio.com/Jio/portal/forgotPassword" TextDecorations="None">
                        <Run Text="Forgot Password" />
                    </Hyperlink>
                </TextBlock>

                <TextBlock x:Name="TbResendOtp" Background="White" Foreground="#FF9B9B9B" HorizontalAlignment="Left" Height="21" Margin="280,80,-17,165" VerticalAlignment="Center" Width="88" Grid.ColumnSpan="2"
                           Grid.Column="0" Visibility="Hidden" >
                    <Hyperlink Foreground="#FF9B9B9B" Background="White" FontSize="13" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" NavigateUri="https://www.jio.com/Jio/portal/forgotPassword" TextDecorations="None">
                        <Run Text="Resend OTP" />
                        <LineBreak></LineBreak>
                    </Hyperlink>
                </TextBlock>


                <TextBlock x:Name="TbGoBack" Foreground="#FF9B9B9B" HorizontalAlignment="Center" Height="28" Margin="132,199,154,39" VerticalAlignment="Center" Width="65" Grid.ColumnSpan="2"
                           Grid.Column="0" Visibility="Hidden" RenderTransformOrigin="0.658,0.571" KeyboardNavigation.TabNavigation="None">
                    <Hyperlink Foreground="#FF9B9B9B" FontSize="14" FontWeight="ExtraBold" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Bold" NavigateUri="https://www.jio.com/Jio/portal/forgotPassword" >
                        <Run Text="Go Back" />
                    </Hyperlink>
                </TextBlock>
            </Grid>
            

            <Button x:Name="BtnLogin" Cursor="Hand" BorderThickness="50" HorizontalAlignment="Center" Content="LOGIN" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" VerticalAlignment="Center" Width="128" Height="35" Margin="240,264,260,139"
                    Grid.Row="0" Grid.RowSpan="2" IsDefault="true" FontSize="15" Foreground="#FFF9F3F3" Visibility="Hidden">
            </Button>
            <Button x:Name="BtnContinue" Cursor="Hand" BorderThickness="50"  HorizontalAlignment="Center" Content="Continue" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" VerticalAlignment="Center" Width="128" Height="35" Margin="240,272,260,131"
                    Grid.Row="0" Grid.RowSpan="2" IsDefault="true" FontSize="15" Foreground="#FFF9F3F3" Click="BtnContinue_Click">
            </Button>
            <TextBlock Grid.Row="0" Height="25" Grid.RowSpan="2" x:Name="PolicyLabel" Margin="147,414,115,-1" Foreground="Gray">
            <TextBlock Width="305" Height="17" Padding="2" FontSize="9" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.544,0.294">By proceeding with login, you agree with
                <Hyperlink x:Name="TermsOfServiceHyperlink" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" FontSize="9" NavigateUri="https://static.jiodrive.com/tos.html" >Terms of service
                <Hyperlink.Style>
                    <Style>
                        <Setter Property="Hyperlink.Foreground" Value="DarkOrange"></Setter>
                        <Style.Triggers>
                            <Trigger Property="Hyperlink.IsMouseOver" Value="True">
                                <Setter Property="Hyperlink.Foreground" Value="#F37F20"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Hyperlink.Style></Hyperlink>
                <TextBlock Padding="2" FontSize="9" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" Text="&" VerticalAlignment="Top"/>
                <Hyperlink x:Name="PrivacyPolicyHyperlink" FontFamily="/JioCloud;component/Views/Fonts/#JioType-Light" FontSize="9" NavigateUri="https://static.jiodrive.com/privacypolicy.html">Privacy policy
                <Hyperlink.Style>
                    <Style>
                        <Setter Property="Hyperlink.Foreground" Value="DarkOrange"></Setter>
                        <Style.Triggers>
                            <Trigger Property="Hyperlink.IsMouseOver" Value="True">
                                <Setter Property="Hyperlink.Foreground" Value="#F37F20"></Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Hyperlink.Style></Hyperlink>
            </TextBlock>
        </TextBlock>
</Grid>
    </Border>
</Window>



What I have tried:

1. I tried to set

Focusable="False"

but it doesn't work.

2. Set

KeyboardNavigation.TabNavigation="None"

It works... BUT, it's a static change. I.e. When I make those hyperlink visible, there is no way to tabindex on them. This is because I can't set KeyboardNavigation.TabNavigation=none dynamically.
how can I set KeyboardNavigation.TabNavigation="None" for hidden elements only.

3. Tried to use TabStop, but it's available for Forms and not for Windows

解决方案

I'm not sure why you would want to do that. Toggling the visibility enables/disables tabstops. You can check it with the following:

<Window

    x:Class="Tabbing.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    mc:Ignorable="d"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    Title="MainWindow" WindowStartupLocation="CenterScreen"

    Height="350" Width="525">
    <Window.Resources>
        <Storyboard x:Key="OnChecked">
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="MyButton">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(ContentControl.Content)" Storyboard.TargetName="toggleButton">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Show"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="OnUnchecked">
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="MyButton">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(ContentControl.Content)" Storyboard.TargetName="toggleButton">
                <DiscreteObjectKeyFrame KeyTime="0" Value="Show"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="toggleButton">
            <BeginStoryboard Storyboard="{StaticResource OnChecked}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="ToggleButton.Unchecked" SourceName="toggleButton">
            <BeginStoryboard Storyboard="{StaticResource OnUnchecked}"/>
        </EventTrigger>
    </Window.Triggers>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
            <Style TargetType="{x:Type StackPanel}">
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
            <Style TargetType="{x:Type ToggleButton}">
                <Setter Property="Padding" Value="10 5"/>
                <Setter Property="Margin" Value="10"/>
            </Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Padding" Value="10 5"/>
                <Setter Property="Margin" Value="10"/>
            </Style>
        </Grid.Resources>

        <TextBox Width="200" />
        <TextBox Width="200" Grid.Row="1"/>
        <TextBox Width="200" Grid.Row="2"/>
        <TextBox Width="200" Grid.Row="3"/>

        <StackPanel Orientation="Horizontal" Grid.Row="4">
            <ToggleButton x:Name="toggleButton" Content="Hide" IsChecked="True"/>
            <Button x:Name="MyButton" Content="HELLO!" Visibility="Visible"/>
        </StackPanel>
    </Grid>
</Window>


Your info was very helpful and solved my issue. I had a TextBlock around a Hyperlink control. I was able to add data binding for Visibility and KeyboardNavigation.TabNavigation in the text control and it works great.

Add to xaml resource at top of page.

<!-- Visibility converters for MVVM values -->
<Helpers:BoolToVisibilityConverter x:Key="BoolToVisibility"/>
<Helpers:BoolToTabNavigationConverter x:Key="BoolToTabNavigation"/>


Use converters in TextBlock

<TextBlock Visibility="{Binding IsLinkVisible, Converter={StaticResource BoolToVisibility}}" KeyboardNavigation.TabNavigation="{Binding IsLinkVisible, Converter={StaticResource BoolToTabNavigation}}">
	<Hyperlink NavigateUri="{Binding url1}">
		<TextBlock Text="{Binding link1}"/>
	</Hyperlink>
</TextBlock>


Covert bool to KeyboardNavigationMode

// bool to KeyboardNavigationMode converter for MVVM classes
[ValueConversion(typeof(bool), typeof(KeyboardNavigationMode))]
public class BoolToTabNavigationConverter : IValueConverter
{
	enum Parameters
	{
		Normal, Inverted
	}
	public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	{
		bool tabStop = (bool)value;
		var direction = Parameters.Normal;
		if (parameter != null)
			direction = (Parameters)Enum.Parse(typeof(Parameters), (string)parameter);
		if (direction == Parameters.Inverted)
			tabStop = !tabStop;
		return tabStop ? KeyboardNavigationMode.Local : KeyboardNavigationMode.None;
	}

	public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	{
		return null;
	}
}


这篇关于如何在WPF中禁用隐藏元素的tabindex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆