可选择应用多种样式

时间:2016-01-22 10:12:36

标签: wpf xaml targettype

让我们说我有一个极其简化的xaml如下:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="converter" />
        <Style TargetType="FrameworkElement" x:Key="maybeCollapsed">
            <Setter Property="Visibility" Value="{Binding Collapsed, Converter={StaticResource converter}}" />
        </Style>
    </UserControl.Resources>
    <StackPanel>
        <Label FontWeight="Bold" Content="Header" x:Name="Header" />
        <TextBox Text="Name" Style="{StaticResource maybeCollapsed}" />
        <TextBox Text="{Binding Name1}" Style="{StaticResource maybeCollapsed}"/>
    </StackPanel>
</UserControl>

如何将第二种样式应用于使用maybeCollapsed样式的所有元素,并将IsTabStop设置为False?我无法在样式本身中执行此操作,因为IsTabStop不是FrameworkElements的成员。

1 个答案:

答案 0 :(得分:2)

使用Control.IsTabStop代替

 <Style TargetType="FrameworkElement" x:Key="maybeCollapsed">
        <Setter Property="Visibility" Value="{Binding Collapsed, Converter={StaticResource converter}}" />
        <Setter Property="Control.IsTabStop" Value="False" />
    </Style>