Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't override a global WPF style that is set by TargetType on a single specific control

I have a style applied to all my textboxes, defined in a resource dictionary..

<Style TargetType="TextBlock">
        <Setter Property="TextBlock.FontSize" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontSize, Mode=OneWay}" />
        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
        <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="TextBox.FontFamily" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontName, Mode=OneWay}"/>
    </Style>\

The fontsize and fontstyle properties are bound to a special user settings class that implements iNotifyPropertyChanged, which allows changes to font size and fontfamily to immediately propogate throughout my application.

However, in a UserControl I've created (Ironically, the screen that allows the user to customize their font settings), I want the font size and fontfamily to remain static. No matter what I try, my global font settings override what I set in my user control:

<UserControl x:Class="ctlUserSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:R2D2" Height="400" Width="600">

<Grid>

    <Grid.Resources>
        <Style x:Key="tbxStyle" TargetType="TextBox">
            <Style.Setters>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontFamily" Value="Tahoma"/>
            </Style.Setters>
        </Style>

... etc...

         <StackPanel Margin="139,122.943,41,0" Orientation="Horizontal" Height="33" VerticalAlignment="Top">
            <TextBox Style="{x:Null}" FontSize="13" FontFamily="Tahoma" HorizontalAlignment="Left" MaxWidth="500" MinWidth="350" Name="txtReaderPath" Height="Auto" VerticalAlignment="Top" />
            <TextBox Style="{x:tbxStyle}" Margin="15,0,0,0" HorizontalAlignment="Left" Name="txtPath" Width="43" Height="23" VerticalAlignment="Top">(some text)</Button>
        </StackPanel>

I've tried setting Style to {x:Null}, setting custom font sizes inline, and setting a style in the resources of this control. None take precedence over the styles in my resource dictionary.

As you can see, I show a sprinkling of all the things I've tried in the XAML sample above...

What am I missing?

like image 251
Matt H. Avatar asked Mar 26 '10 16:03

Matt H.


2 Answers

Another alternative is to add an empty style on the UserControl such as:

<UserControl.Resources>
        <Style TargetType="TextBlock" />
</UserControl.Resources>

That blocks the global style - can also add it to the StackPanel resources for finer grained control

  • longer exmplanation here
like image 137
Richard Avatar answered Nov 11 '22 06:11

Richard


Ok I see what is happening now.

In the constructor for my UserControl, I even tried setting .Style=Nothing, just to see what would happen... and my global style was STILL coming along and applying itself.

I've got to assume that the global style is being applied to my controls AFTER the constructor runs, and as such the global style intrudes anyway. In the end, I've simply moved the 'global' style to a location where it will only effect what I need it to.

Less convenient, but at least I can move on in my project

like image 39
Matt H. Avatar answered Nov 11 '22 06:11

Matt H.