My problem occurs with WPF in .NET 3.5 SP1 and can be described as follows:
I have a default Style hitting all TextBlock elements in my user interface. Here's what it looks like:
<Style TargetType="{x:Type TextBlock}"> <Setter Property="TextTrimming" Value="CharacterEllipsis"/> <Setter Property="Foreground" Value="Red"/> </Style>
This works great for all TextBlock s. In addition to this, I have a Button style that includes a ControlTemplate that looks like this (shortened):
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}" BasedOn="{x:Null}"> <Setter Property="Foreground" Value="Green"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="Border" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Height="24" BorderBrush="{TemplateBinding BorderBrush}"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" TextBlock.Foreground="{TemplateBinding Foreground}"/> </Border> <ControlTemplate.Triggers>...</ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
Notice the line TextBlock.Foreground="{TemplateBinding Foreground}" in ContentPresenter . This should cause the button text to turn green, and in fact it will look in the designer of Visual Studio. But when I compile and run the program, the button text turns red, the text color is set to the default TextBlock . I tested this with Snoop.
How can I prevent the default TextBlock style from overriding the value of TextBlock.Foreground ? The OverridesDefaultStyle ContentPresenter property does not help in this case.
Any idea?
source share