I have a WPF application with two xaml windows, one named MainWindow.xaml
and other is addNewsWindow.xaml
.
In MainWindow.xaml
I have a DocumentViewer
and a button named Add News
that takes me to the other window named AddNewsWindow.xaml
.
This is my DocumentViewer
control in MainWindow.xaml
:
<DocumentViewer x:FieldModifier="public" x:Name="docViwer"
Grid.Row="2" Grid.RowSpan="4" Grid.ColumnSpan="4"
BorderBrush="Black" BorderThickness="1"
Margin="1,2,40,1">
On my addNewsWindow.xaml
I have lots of controls to take user input and a button to browse and select word file, which to be displayed in the DocumentViewer
in the MainWindow.xaml
:
The problem:
When coding the click event for the Add button in the addNewsWindow.xaml
(which when pressed should take the word file convert it to XPS and put in the Document viewer in MainWindow
), I can't reference to the MainWindow DocumentViewer
and put the converted XPS file into the DocumentViewer
.
AddNewsWindow.cs
private void FilePathBtn_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".doc";
dlg.Filter = "Word documents|*.doc;*.docx";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
filePathTBox.Text = filename;
}
}
private XpsDocument ConvertWordToXps(string wordFilename, string xpsFilename)
{
// Create a WordApplication and host word document
Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
try
{
wordApp.Documents.Open(wordFilename);
// To Invisible the word document
wordApp.Application.Visible = false;
// Minimize the opened word document
wordApp.WindowState = WdWindowState.wdWindowStateMinimize;
Document doc = wordApp.ActiveDocument;
doc.SaveAs(xpsFilename, WdSaveFormat.wdFormatXPS);
XpsDocument xpsDocument = new XpsDocument(xpsFilename, FileAccess.Read);
return xpsDocument;
}
catch (Exception ex)
{
MessageBox.Show("Error occurs, The error message is " + ex.ToString());
return null;
}
finally
{
wordApp.Documents.Close();
((_Application)wordApp).Quit(WdSaveOptions.wdDoNotSaveChanges);
}
}
private void AddNewsBtn_Click(object sender, RoutedEventArgs e)
{
string wordDocument = filePathTBox.Text;
if (string.IsNullOrEmpty(wordDocument) || !File.Exists(wordDocument))
{
MessageBox.Show("The file is invalid. Please select an existing file again.");
}
else
{
string convertedXpsDoc = string.Concat(System.IO.Path.GetTempPath(), "\\", Guid.NewGuid().ToString(), ".xps");
XpsDocument xpsDocument = ConvertWordToXps(wordDocument, convertedXpsDoc);
if (xpsDocument == null)
{
return;
}
// MainWindow.docViewer = xpsDocument.GetFixedDocumentSequence();
docViewer.Document = xpsDocument.GetFixedDocumentSequence();
}
}
I get the error:
The name docViewer does not exist in the current context
I don't know how to reference the DocumentViewer
in MainWindow
from the AddnewsWindow.cs
It's not clear how you're creating and showing the
AddNewsWindow
. It may be possible for you to pass theMainWindow
to theAddNewsWindow
(viathis
):The
MainWindow
could define a method to update the document:Then, from
AddNewsWindow
, you can call:Alternatively, instead of passing the
MainWindow
to theAddNewsWindow
, you may be able to obtain theMainWindow
directly:The reason you need to cast it is that
Application.Current.MainWindow
returns an instance ofWindow
, not yourMainWindow
. You'll needMainWindow
in order to call itsSetDocument
method.