简体   繁体   中英

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.

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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM