1

在我的 app.xaml 中:

<Application.Resources>
    <SolidColorBrush x:Key="colorBrush1" Color="Orange" Opacity="1"/>
    <SolidColorBrush x:Key="colorBrush2" Color="Green" Opacity="1"/>
</Application.Resources>

在我的代码隐藏中:

Run run = new Run("My name is Bob!");
run.SetResourceReference(ForegroundProperty, "colorBrush1");
run.SetResourceReference(BackgroundProperty, "colorBrush2");

Paragraph paragraph = new Paragraph(run);

this.flowDocument.Blocks.Add(paragraph);

预期结果:运行显示为上面 app.xaml 中定义的前景色和背景色。

实际结果:前景色有效(显示为橙色),但背景保持透明。

为什么不将运行的背景绑定到资源工作,就像它与前景一样??? 我尝试先将运行和段落添加到FlowDocument,然后再绑定,但结果是一样的。

4

1 回答 1

1

原因是BackgroundProperty需要明确。这就是你想要的:

        Run run = new Run("My name is Bob!");
        run.SetResourceReference(Run.ForegroundProperty, "colorBrush1");
        run.SetResourceReference(Run.BackgroundProperty, "colorBrush2");

一个真正的谜团是为什么只写“ForegroundProperty”就可以了。

于 2011-05-23T17:57:37.580 回答