Search code examples
wpfflowdocumentdata-binding

FlowDocumentReader Document doesn't get notified when bound source changes, why?


So I have this XAML in the .xaml file

<StackPanel>
        <Button Width="200" Height="30" Content="Change Words" 
                Click="Button_Click"/>
        <FlowDocumentReader 
            ViewingMode="Scroll" Zoom="90"
            Focusable="True"
            Background="White"
            IsFindEnabled="True"
            IsPageViewEnabled="True"
            IsScrollViewEnabled="True"
            x:Name="FDR"
            Document="{Binding Path=WordDocument}"
            Width="400" Height="400">            
        </FlowDocumentReader>
    </StackPanel>

And in the code behind, On load,

public partial class Window1 : Window
    {
        MyDoc _myDoc = null;
        FlowDocument _theFlowDocument;

        public Window1()
        {
            InitializeComponent();
            _myDoc  = new MyDoc().Create(); // Create returns MyDoc, that has a WordDocument property with some FlowDocument contents
            this.DataContext = _myDoc ;
        }
 private void Button_Click(object sender, RoutedEventArgs e)
        {
            _myDoc.WordDocument = _myDoc.CreateFlowDocument("Now it's changed");
        }
 }

On button click, I am changing the contents of the WordDocument. The CreateFlowDocument creates a Paragraph and a Run with the string passed.

When button is clicked, the FlowDocumentReader doesn't show the changed contents, although I've bound it to the WordDocument property

What am I doing wrong?


Solution

  • How do you implement WordDocument property? It either needs to be a dependency property, or you need to implement INotifyPropertyChanged and raise PropertyChanged event accordingly when you change the property value, or you need to add a WordDocumentChanged event to your class and raise that when you change the value. If it's just a plain property, there's no way for binding expression to detect when the value changes at run-time.