Can't override a global WPF style that is set by TargetType on a single specific control
Posted
by Matt H.
on Stack Overflow
See other posts from Stack Overflow
or by Matt H.
Published on 2010-03-26T16:22:59Z
Indexed on
2010/03/26
16:43 UTC
Read the original article
Hit count: 439
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?
© Stack Overflow or respective owner