WPF Style does not affect certain properties
I've got a Style
specified for Paragraph
as part of my FlowDocumentReader
's Resources section:
<FlowDocumentReader>
<FlowDocumentReader.Resources>
<Style x:Key="myStyle" TargetType="{x:Type Paragraph}">
<Setter Property="Foreground" Value="LightSteelBlue" />
<Setter Property="BorderBrush" Value="LightSteelBlue" />
<Setter Property="BorderThickness" Value="1.0" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="{Binding Path=MyFontSize}" />
</Style>
</FlowDocumentReader.Resources>
</FlowDocumentReader>
I've开发者_开发知识库 got a .xaml file which contains my FlowDocument
and it has some Paragraph
s which are defined as so:
<Paragraph Style='{DynamicResource myStyle}">
Stuff here
</Paragraph>
The problem that I have is that Foreground
doesn't apply to the text (it shows as Black rather than LightSteelBlue) and the FontSize
does not change when the MyFontSize
property is modified.
I've checked the property value in the code behind and it is set but it doesn't result in a change in the UI.
This seems to only be an issue with the FlowDocument
if it is loaded into the FlowDocumentReader
at runtime. If the XAML is explicitly placed inside the FlowDocumentReader
in the .xaml file, the Foreground
is the correct color and the FontSize
changes based on the property setting.
Ideas?
Solved:
As I wrote in my answer below, by moving the Style
block into the Resources section of the FlowDocument
itself resolves the issue.
Have you tried setting foreground for your paragraph directly? it must be a different propety/attached property which manages contents foreground.
Well, I solved this problem by moving the Style blocks out of the FlowDocumentReader Resources and into the Resources section of the FlowDocument itself. The resulting FlowDocument looks something like so:
<FlowDocument>
<FlowDocument.Resources>
<Style x:Key="myStyle" TargetType="{x:Type Paragraph}">
<Setter Property="Foreground" Value="LightSteelBlue" />
<Setter Property="BorderBrush" Value="LightSteelBlue" />
<Setter Property="BorderThickness" Value="1.0" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="{Binding Path=MyFontSize}" />
</Style>
</FlowDocument.Resources>
<Paragraph Style="{DynamicResource myStyle}">
Stuff here
</Paragraph>
</FlowDocument>
精彩评论