title: How to force FlowDocument refresh its content on DataContext set?
body: <p>I'm trying to use FlowDocument with bindings to have separate template which is able to be filled with actual data from data model. Then I convert it to image and print or save on hard-drive.
  For binding FlowDocument's Runs with data model I use the code from this article: <a href="https://msdn.microsoft.com/en-us/magazine/dd569761.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/magazine/dd569761.aspx</a></p>
  
  <p>The FlowDocument template is following:</p>
  
  <pre><code>&lt;FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:p="clr-namespace:Labels;assembly=DataModel.Impl"
  PageWidth="200" MinPageWidth="200" PageHeight="200" MinPageHeight="200"&gt;
  
  &lt;Section&gt;
    &lt;Paragraph&gt;
      &lt;p:BindableRun BoundText="{Binding Path=Text}"/&gt;
    &lt;/Paragraph&gt;
  &lt;/Section&gt;
  
  &lt;/FlowDocument&gt;
  </code></pre>
  
  <p>Code for BindableRun:</p>
  
  <pre><code>public class BindableRun : Run
    {
      public static readonly DependencyProperty BoundTextProperty = DependencyProperty.Register("BoundText", typeof(string), typeof(BindableRun),
        new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, OnBoundTextChanged, CoerceText));
  
      public BindableRun()
      {
        FlowDocumentHelpers.FixupDataContext(this);
      }
  
      private static void OnBoundTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
        ((Run)d).Text = (string)e.NewValue;
      }
  
      private static object CoerceText(DependencyObject d, object value)
      {
        return value;
      }
  
      public String BoundText
      {
        get { return (string)GetValue(BoundTextProperty); }
        set { SetValue(BoundTextProperty, value); }
      }
    }
  </code></pre>
  
  <p>Then I load template and set DataContext in it:</p>
  
  <pre><code>private class DataClass
  {
    public string Text { get; set; }
  }
  
  private static FlowDocument LoadFlowDocument(string path)
  {
    using (var xamlFile = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
      return XamlReader.Load(xamlFile) as FlowDocument;
    }
  }
  
      private static void FlowDoc2Image(FlowDocument document, DataClass dataContext, Stream imageStream)
  {
    var flowDocumentScrollViewer = new FlowDocumentScrollViewer
    {
      VerticalScrollBarVisibility = ScrollBarVisibility.Hidden,
      HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden,
      DataContext = dataContext
    };
  
    flowDocumentScrollViewer.Document = document;
  
    flowDocumentScrollViewer.Measure(new Size(999999999, 999999999));
  
    //1st pass
    flowDocumentScrollViewer.Arrange(new Rect(0, 0, flowDocumentScrollViewer.ActualWidth, flowDocumentScrollViewer.ActualHeight));
    //2nd pass. It's not code duplication! Do not remove!
    flowDocumentScrollViewer.Arrange(new Rect(0, 0, flowDocumentScrollViewer.ActualWidth, flowDocumentScrollViewer.ActualHeight));
  
    var bitmapRenderer =
      new RenderTargetBitmap((int)flowDocumentScrollViewer.ActualWidth, (int)flowDocumentScrollViewer.ActualHeight, 96, 96, PixelFormats.Pbgra32);
  
    bitmapRenderer.Render(flowDocumentScrollViewer);
  
    var pngEncoder = new PngBitmapEncoder { Interlace = PngInterlaceOption.On };
    pngEncoder.Frames.Add(BitmapFrame.Create(bitmapRenderer));
  
    pngEncoder.Save(imageStream);
  }
  
  public void Test()
  {
    var doc = LoadFlowDocument("C:\\Experiments\\DocWithBinding.xaml");
  
    var context = new DataClass {Text = "SomeText"};
    doc.DataContext = context;
  
    using (var imageStream = new FileStream("C:\\Experiments\\image.png", FileMode.OpenOrCreate, FileAccess.Write))
    {
      FlowDoc2Image(doc, context, imageStream);
    }
  }
  </code></pre>
  
  <p>But nothing happens. I tried to set break points in BindableRun on changing it's value. And I never get there. Changing DataContext doesn't affect the document.</p>