WPF 样式不影响某些属性
我已经为 Paragraph
指定了 Style
作为我的 FlowDocumentReader
资源部分的一部分:
<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>
我有一个 .xaml 文件,其中包含我的 FlowDocument
,它有一些 Paragraph
定义如下:
<Paragraph Style='{DynamicResource myStyle}">
Stuff here
</Paragraph>
我遇到的问题是 Foreground
不适用于文本(它显示为 Black 而不是 LightSteelBlue),并且当修改 MyFontSize
属性时,FontSize
不会更改。
我已经检查了后面代码中的属性值,它已设置,但不会导致 UI 发生更改。
如果在运行时将 FlowDocument
加载到 FlowDocumentReader
中,这似乎只是 FlowDocument
的问题。如果 XAML 显式放置在 .xaml 文件中的 FlowDocumentReader
内,则 Foreground
是正确的颜色,并且 FontSize
根据属性更改环境。
有想法吗?
已解决:
正如我在下面的回答中所写,通过将 Style
块移动到 FlowDocument
本身的资源部分可以解决该问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过直接为段落设置前景?它必须是管理前景内容的不同属性/附加属性。
Have you tried setting foreground for your paragraph directly? it must be a different propety/attached property which manages contents foreground.
好吧,我通过将样式块从 FlowDocumentReader 资源中移出并移到 FlowDocument 本身的资源部分中解决了这个问题。生成的 FlowDocument 看起来像这样:
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: