Exar666Kun
Exar666Kun

Reputation: 135

A Control does not use a defined Style but another does

I have a problem with Styles in XAML, and maybe you could help me.

i have created a ResourceDictionary "controldefaultstyle":

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Control}" x:Key="ControlDefaultStyle" >
    <Style.Setters>
        <Setter Property="FontFamily" Value="{Binding Path=FontFamily, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged}"/>
        <Setter Property="FontSize" Value="{Binding Path=FontSize, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged}"/>
        <Setter Property="Background" Value="{StaticResource SystemBackground}"/>
        <Setter Property="Foreground" Value="{StaticResource SystemForeground}"/>
    </Style.Setters>
</Style>

than i made two other ResourceDictionaries, One which bases on the button and the controldefaultstyle:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:ext="clr-namespace:StyleResourceDictionariesDemo.ResourceDictionaries.Classes">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ControlDefaultStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" x:Key="ButtonStyle" >
    <Style.BasedOn>
        <ext:MergedStyles BasedOn="{StaticResource {x:Type Button}}" MergeStyle="{StaticResource ControlDefaultStyle}"/>
    </Style.BasedOn>
    <Style.Setters>
        <Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}" >
                    <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">
                        <Rectangle x:Name="buttonFrame" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                               Stroke="{TemplateBinding Background}" RadiusX="5" RadiusY="5" StrokeThickness="1" Fill="Transparent"/>
                        <Rectangle x:Name="buttonBody" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                               Stroke="Transparent" RadiusX="5" RadiusY="5" StrokeThickness="1" Fill="{TemplateBinding Background}"/>
                        <TextBlock x:Name="buttonText" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Content}" 
                                   Foreground="{TemplateBinding Foreground}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource IsMouseOverBackground}"/>
            <Setter Property="Foreground" Value="{StaticResource IsMouseOverForeground}"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" Value="{StaticResource IsPressedBackground}"/>
            <Setter Property="Foreground" Value="{StaticResource IsPressedForeground}"/>
        </Trigger>
    </Style.Triggers>
</Style>

and another on which uses Textblock and the ControlDefaultStyle:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ControlDefaultStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyle"  >
    <Style.BasedOn>
        <classes:MergedStyles BasedOn="{StaticResource {x:Type TextBlock}}" MergeStyle="{StaticResource ControlDefaultStyle}"/>
    </Style.BasedOn>

    <Style.Setters>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="150"/>
        <Setter Property="TextAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Top"/>
        <!--<Setter Property="Background" Value="{StaticResource TextBlockBackground}"/>-->
        <!--<Setter Property="Foreground" Value="{StaticResource TextBlockForeground}"/>-->
    </Style.Setters>
</Style>

when using the button style on a button, everything works as expected and the colors changes as wished, but the textblock does not change the background and i don't get why. Textblock and Button should look the same (for background and foreground)

Any Conclusion?

Kind Regards Mirko

edit: left correct, right background should be blue and not white.

Changing Fontfamily (Combobox) and Size (Slider controlled), are working for both the buttons and the Textblock.

enter image description hereenter image description here

Upvotes: 0

Views: 47

Answers (1)

Andy
Andy

Reputation: 12276

TextBlock does not inherit from control.

Take a look at the inheritance chain:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.textblock?view=netframework-4.8

Object DispatcherObject DependencyObject Visual UIElement FrameworkElement TextBlock

That's why your base style targeting control will not be applied to a textblock.

Upvotes: 2

Related Questions