【问题标题】:WPF Style does not affect certain propertiesWPF 样式不影响某些属性
【发布时间】:2011-06-15 22:39:23
【问题描述】:

我为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 并且它有一些 Paragraphs 定义如下:

<Paragraph Style='{DynamicResource myStyle}">
    Stuff here
</Paragraph>

我遇到的问题是 Foreground 不适用于文本(它显示为 Black 而不是 LightSteelBlue),并且 FontSize 在修改 MyFontSize 属性时不会改变。

我检查了后面代码中的属性值,它已设置,但不会导致 UI 发生变化。

这似乎只是FlowDocument 在运行时加载到FlowDocumentReader 时的问题。如果 XAML 明确放置在 .xaml 文件中的 FlowDocumentReader 内,则 Foreground 是正确的颜色,FontSize 会根据属性设置而变化。

想法?


已解决:

正如我在下面的回答中所写,通过将 Style 块移动到 FlowDocument 的资源部分本身可以解决问题。

【问题讨论】:

    标签: wpf xaml binding styles dynamicresource


    【解决方案1】:

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

    【讨论】:

    • 是的,如果我设置了paragraph.Foreground = new SolidColorBrush(Colors.Red),它会改变颜色。
    【解决方案2】:

    好吧,我通过将 Style 块从 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2014-11-08
      • 2015-10-20
      • 2012-04-25
      • 2017-02-25
      • 1970-01-01
      相关资源
      最近更新 更多