我正在尝试创建一个触发器,当TextBlock
为Visibility
时,会从标签导航中删除Collapsed
。
这是风格:
<Style x:Uid="Style_1" TargetType="TextBlock">
<Setter x:Uid="Setter_1" Property="TextOptions.TextFormattingMode" Value="{StaticResource TextFormattingMode}"/>
<Setter x:Uid="Setter_32" Property="TextOptions.TextRenderingMode" Value="{StaticResource TextRenderingMode}"/>
<Setter x:Uid="Setter_2" Property="TextBlock.FontFamily" Value="{StaticResource FontFamily}"/>
<Setter x:Uid="Setter_3" Property="SnapsToDevicePixels" Value="True"/>
<Setter x:Uid="Setter_4" Property="VerticalAlignment" Value="Center"/>
<Setter x:Uid="Setter_74" Property="UseLayoutRounding" Value="True"/>
<Setter x:Uid="Setter_5" Property="Foreground" Value="{StaticResource LabelForeground}"/>
<Setter x:Uid="Setter_7" Property="FontSize" Value="{StaticResource FontSize}"/>
<Style.Triggers>
<DataTrigger x:Uid="DataTrigger_2" Binding="{Binding Source={x:Static cs:ZoomLevel.Instance}, Path=ActualZoomLevelIsDefault}" Value="False">
<Setter x:Uid="Setter_33" Property="TextOptions.TextFormattingMode" Value="Ideal"/>
</DataTrigger>
<DataTrigger x:Uid="DataTrigger_100" Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Visibility}" Value="Collapsed">
<!--<Setter Property="IsEnabled" Value="False"/>-->
<!--<Setter Property="Control.IsTabStop" Value="False"/>-->
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
但是如果我选择它,TextBlock仍然会获得焦点。当我专注于TextBlock时,IsKeyboardFocusWithin为true。我可以在触发器中将IsEnabled设置为false以使其工作,但我想知道为什么附加属性不起作用。所以我的问题是:为什么KeyboardNavigation属性不起作用?
示例:
<TextBlock x:Uid="TextBlock_2" Grid.Column="1" Grid.Row="1" Margin="3,3,0,0" HorizontalAlignment="Right" Visibility="Collapsed">
<Hyperlink x:Uid="Hyperlink_2" Command="{Binding SelectRoutingMethods}">
<Run x:Uid="Run_2" Text="{Binding ContactPreferences}"/>
</Hyperlink>
</TextBlock>
TextBlock是可列表的,但在我的示例中仍然可见。
答案 0 :(得分:1)
不是你的TextBlock
得到了关注。这是超链接。我不知道为什么,它甚至不在Visual Tree中,但确实如此。这似乎是个bug。如果将其属性Focusable
设置为false,则不再需要关注。
以下是完整的简化示例:
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="Focusable" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Visibility}" Value="Collapsed">
<Setter Property="Focusable" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox>Temp</TextBox>
<TextBlock Visibility="Visible" Grid.Row="1">
<Hyperlink Focusable="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBlock}, Path=Focusable}" Command="{Binding AddOptionalAddressCommand}">test</Hyperlink>
</TextBlock>
<TextBox Grid.Row="2" >Temp</TextBox>
</Grid>
答案 1 :(得分:0)
正如@Shadowed回答的那样,Hyperlink
获得Focus
。不确定为什么会发生这种情况,因为如果父级处于Content
状态,则Focus
不应该Collapsed
。
无论如何,我可以为你解决这个问题。
<StackPanel>
<StackPanel.Resources>
<local:VisibilitytoFocusConverter x:Key="VisibilitytoFocusConv" />
</StackPanel.Resources>
<TextBox >Temp</TextBox>
<TextBlock Visibility="Collapsed" Height="20" KeyboardNavigation.TabNavigation ="{Binding Visibility, RelativeSource={RelativeSource Self}, Converter={StaticResource VisibilitytoFocusConv}}">
<Hyperlink Command="{Binding AddOptionalAddressCommand}" />
</TextBlock>
<TextBox >Temp</TextBox>
</StackPanel>
<强>转换器:强>
public class VisibilitytoFocusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo language)
{
return ((Visibility)value) == Visibility.Visible ? "Local" : "None";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)
{
return new NotFiniteNumberException();
}
}
希望有所帮助。
答案 2 :(得分:0)
@Shadowed因发现超链接问题而致记。
以下是我的替代解决方案:根据树的下一个KeyboardNavigation.IsTabStop
可见性在Hyperlink
上设置UIElement
(在此特定情况下,这将是TextBlock
。
<Hyperlink x:Uid="Hyperlink_2" KeyboardNavigation.IsTabStop="{Binding IsVisible,RelativeSource={RelativeSource AncestorType={x:Type UIElement}}}">