Третья и последняя статья про просмотр документов MS Word и Excel из WPF-приложения.
Сделаем тестовое приложение, в котором можно будет походить по папкам и пооткрывать документы.
XAML окна
<Window x:Class="OfficeControlsTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodels="clr-namespace:OfficeControlsTestApp.ViewModels"
xmlns:controls="clr-namespace:OfficeControlsTestApp.Controls"
xmlns:excel="clr-namespace:WPFExcelControl;assembly=WPFExcelControl"
xmlns:word="clr-namespace:WPFWordControl;assembly=WPFWordControl"
Title="Просмотр документов Word Excel"
Icon="/OfficeControlsTestApp;component/Images/viewer.png"
Height="400" Width="800">
<Window.DataContext>
<viewmodels:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300px" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<controls:TreeViewFolderBrowser Grid.Column="0" File="{Binding BrowserFile, Mode=OneWayToSource}" />
<GridSplitter Grid.Column="1"
Width="5"
Background="LightBlue"
VerticalAlignment="Stretch" HorizontalAlignment="Center" />
<Canvas Grid.Column="2" x:Name="canvas">
<excel:Excel File="{Binding ExcelFile, Mode=OneWay}">
<excel:Excel.Resources>
<Style TargetType="{x:Type excel:Excel}">
<Setter Property="Canvas.Left" Value="0" />
<Setter Property="Canvas.Top" Value="0" />
<Setter Property="Width" Value="{Binding ElementName=canvas, Path=ActualWidth, Mode=OneWay}" />
<Setter Property="Height" Value="{Binding ElementName=canvas, Path=ActualHeight, Mode=OneWay}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ExcelFile, Mode=OneWay}" Value="">
<Setter Property="Canvas.Left" Value="-100" />
<Setter Property="Canvas.Top" Value="-100" />
<Setter Property="Width" Value="10" />
<Setter Property="Height" Value="10" />
</DataTrigger>
</Style.Triggers>
</Style>
</excel:Excel.Resources>
</excel:Excel>
<word:Word File="{Binding WordFile, Mode=OneWay}">
<word:Word.Resources>
<Style TargetType="{x:Type word:Word}">
<Setter Property="Canvas.Left" Value="0" />
<Setter Property="Canvas.Top" Value="0" />
<Setter Property="Width" Value="{Binding ElementName=canvas, Path=ActualWidth, Mode=OneWay}" />
<Setter Property="Height" Value="{Binding ElementName=canvas, Path=ActualHeight, Mode=OneWay}" />
<Style.Triggers>
<DataTrigger Binding="{Binding WordFile, Mode=OneWay}" Value="">
<Setter Property="Canvas.Left" Value="-100" />
<Setter Property="Canvas.Top" Value="-100" />
<Setter Property="Width" Value="10" />
<Setter Property="Height" Value="10" />
</DataTrigger>
</Style.Triggers>
</Style>
</word:Word.Resources>
</word:Word>
</Canvas>
</Grid>
</Window>
Так как Z-index отказывается работать как ожидается, пришлось сделать переключение между Word и Excel немного необычным способом - прятать ненужное приложение за границу экрана, а необходимое в данный момент выводить на экран.