Search code examples
c#mvvm-lightdocumentviewer

Using a DocumentViewer to view .docx documents in MVVM Light


I've got a relaycommand that fires off and I'm trying to set the DocumentViewer to Show documents in MVVM When doing this is WPF this is fairly straight foward but I have to do this in the view model. Using the code below currently nothing is happening....

XAML Code :

<DocumentViewer HorizontalAlignment="Left" Margin="30,10,0,0"
                Name="documentViewer1" VerticalAlignment="Top" Height="200" Width="600" />

ViewModel Code :

   private void FileChooser()
   {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.DefaultExt = ".docx";
        dlg.Filter = ".Docx Files (*.docx)|*.docx";
        Nullable<bool> result = dlg.ShowDialog();
        if (result == true)
        {

            string fileName = dlg.FileName;

            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            string newXPSDocumentName = String.Concat(System.IO.Path.GetDirectoryName(dlg.FileName), "\\",
                       System.IO.Path.GetFileNameWithoutExtension(dlg.FileName), ".xps");
            DocText = ConvertWordDocToXPSDoc(dlg.FileName, newXPSDocumentName).GetFixedDocumentSequence();
         }
    }

PropertyChangedMethod in the View Model for the DocumentViewer :

    private IDocumentPaginatorSource _docText;
    public IDocumentPaginatorSource DocText
    {
        get
        {

            return _docText;
        }

        set
        {
            _docText = value;
            RaisePropertyChanged("DocText");
        }
    }

No Document is shown whatsoever and no error any help would be appreciated.


Solution

  • As an answer to my problem and a warning of coding when you're tired.... Once I came back and viewed my code the problem lies in my xaml in that I forgot to Bind my logic. Always the small stuff.... So the correct xaml would be :

    <DocumentViewer HorizontalAlignment="Left" Margin="30,10,0,0"
                    Document="{Binding Path=DocText, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Height="200" Width="600" />