Search code examples
wpfxamltargettype

Optionally apply multiple styles


Let's say I have a extremly simplified xaml as follows:

<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>

How could I apply a second style to all elements that use the maybeCollapsed-style, setting the IsTabStopto False? I cannot do this in the style itself, as IsTabStop is not a member of FrameworkElements.


Solution

  • Use Control.IsTabStop instead

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