WPF 样式不影响某些属性

发布于 2024-10-14 13:09:31 字数 1476 浏览 8 评论 0原文

我已经为 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 Paragraphs 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤凫 2024-10-21 13:09:32

您是否尝试过直接为段落设置前景?它必须是管理前景内容的不同属性/附加属性。

Have you tried setting foreground for your paragraph directly? it must be a different propety/attached property which manages contents foreground.

榕城若虚 2024-10-21 13:09:32

好吧,我通过将样式块从 FlowDocumentReader 资源中移出并移到 FlowDocument 本身的资源部分中解决了这个问题。生成的 FlowDocument 看起来像这样:

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文