Search code examples
wpf

css !Important in WPF style


I have text block with MaxWidth="80" and want change on Style without remove MaxWidth="80"

the TextBlock is at third party control for sample:

 <Window.Resources>
        <Style TargetType="TextBox" >
            <Setter Property="Foreground" Value="Red" />
            <Setter Property="FontSize" Value="10"/>

        </Style>
    </Window.Resources>

    <Grid>
        <TextBox  FontSize="45" Foreground="Blue" Text="OH My God"/>
    </Grid>

Solution

  • In the WPF there is a strict rule which property or style is used.

    Attribute has more power than a style.

    You can solve this by order:

    • Define a default style with the property set as 100.
    • Define an new style that is based on the default and override the property value for your control and apply newly created style to your TextBox.