Default TextBlock Text Style for Text Above Color

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?

+4
source share
2 answers

See answer 5 to this link

This is because ContentPresenter creates a TextBlock for the contents of the string, and since the TextBlock is not in the visual tree, it will look for the application level resource. And if you define the style for the text block in the application level, then it will be applied to these TextBlocks in the ContentControl

The workaround is to define a DataTemplate for System.String, where we can explicitly use the default TextBlock to display the content. You can put this DataTemplate in the same dictionary that you define a TextBlock so that this DataTemplate will apply to whatever ContentPresenter your style is.

Try adding this to a ResourceDictionary

 <DataTemplate DataType="{x:Type sys:String}"> <TextBlock Text="{Binding}"> <TextBlock.Resources> <Style TargetType="{x:Type TextBlock}"/> </TextBlock.Resources> </TextBlock> </DataTemplate> 
+10
source

You better not override the default style for TextBlock. The best idea that I could come up with so far is to create a style for managing and applying it to all top-level windows.

 <!-- App.xaml --> <Application.Resources> <Style x:Key="RedStyle" TargetType="{x:Type Control}"> <Setter Property="TextTrimming" Value="CharacterEllipsis"/> <Setter Property="Foreground" Value="Red"/> </Style> </Application.Resources> <!-- MainWindow.xaml --> <Window Style="{StaticResource RedStyle}" ...> ... </Window> 

See here http://www.ikriv.com/dev/dotnet/wpftextstyle/ for more details.

+4
source

Source: https://habr.com/ru/post/1332902/


All Articles