views:

242

answers:

1

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?

+1  A: 

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.

Pavel Minaev
Good explaination, I had a typo with my INPC property name in the event handling. Not sure how I missed it.
Vin
Yeah, I hate it when it happens. It's one of the reasons why I generally prefer having `...Changed` event for every property instead - it's more verbose, sure, but you don't have to deal with string literals anywhere, so there are no typos. For the future, you might be interested in looking at (and hopefully voting for) the following two MS Connect tickets: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=348849 and https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=370313
Pavel Minaev
Use the ObservableObject in the MVVM foundation project to avoid mismatched property changed handlers. Http://mvvmfoundation.codeplex.com
Anderson Imes