WPF StackPanel.Resources setter for multiple control types?

I want to use setter to set the default field for all elements in my stack panel, not just buttons, but also text fields and labels.

    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Margin" Value="0,10,0,0"/>
            </Style>
        </StackPanel.Resources>
        ...

When I try to change the above Button to Controlor FrameworkElement(the derived type of each element), it does not work.

How can I fix this without specifying 2 different elements Stylewith the same content, but different x: Types in TargetType?

+3
source share
1 answer

You can do this with inheritance via the Style BasedOn attribute :

        <StackPanel.Resources>
            <Style x:Key="BaseStyle" TargetType="{x:Type FrameworkElement}">
                <Setter Property="Margin" Value="0,10,0,0"/>
            </Style>

            <Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseStyle}" />
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}" />
            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}" />

        </StackPanel.Resources>
+7
source

Source: https://habr.com/ru/post/1792554/


All Articles