In my app.xaml:
<Application.Resources>
<SolidColorBrush x:Key="colorBrush1" Color="Orange" Opacity="1"/>
<SolidColorBrush x:Key="colorBrush2" Color="Green" Opacity="1"/>
</Application.Resources>
In my code-behind:
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);
Expected Result: Run appears with foreground color and background color as defined in app.xaml above.
Actual Result: Foreground color works (appears orange), but background remains transparent.
Why doesn't binding the run's background to a resource work, like it does with foreground??? I tried adding the run and paragraph to the FlowDocument first and then binding, but the result was the same.
The reason is that BackgroundProperty needs to be clarified. This is what you want:
Run run = new Run("My name is Bob!");
run.SetResourceReference(Run.ForegroundProperty, "colorBrush1");
run.SetResourceReference(Run.BackgroundProperty, "colorBrush2");
A real mystery is why just writing "ForegroundProperty" works.