■ ControlTemplate 엘리먼트를 사용해 DocumentViewer 엘리먼트를 정의하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Color x:Key="ControlLightColor">White</Color> <Color x:Key="ControlMediumColor">#ff7381f9</Color> <Style x:Key="{x:Type DocumentViewer}" TargetType="{x:Type DocumentViewer}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey }}" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DocumentViewer}"> <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Focusable="False"> <Grid KeyboardNavigation.TabNavigation="Local"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.Background> <SolidColorBrush Color="{DynamicResource ControlLightColor}" /> </Grid.Background> <ToolBar ToolBarTray.IsLocked="True" KeyboardNavigation.TabNavigation="Continue"> <Button CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Command="ApplicationCommands.Print" Content="Print" /> <Button Command="ApplicationCommands.Copy" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Content="Copy" /> <Separator /> <Button Command="NavigationCommands.IncreaseZoom" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Content="Zoom In" /> <Button Command="NavigationCommands.DecreaseZoom" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Content="Zoom Out" /> <Separator /> <Button Command="NavigationCommands.Zoom" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="100.0" Content="Actual Size" /> <Button Command="DocumentViewer.FitToWidthCommand" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Content="Fit to Width" /> <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="1" Content="Whole Page" /> <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="2" Content="Two Pages" /> </ToolBar> <ScrollViewer Name="PART_ContentHost" Grid.Row="1" CanContentScroll="true" HorizontalScrollBarVisibility="Auto" IsTabStop="true"> <ScrollViewer.Background> <LinearGradientBrush StartPoint="0.5 0" EndPoint="0.5 1"> <GradientStop Offset="0" Color="{DynamicResource ControlLightColor }" /> <GradientStop Offset="1" Color="{DynamicResource ControlMediumColor}" /> </LinearGradientBrush> </ScrollViewer.Background> </ScrollViewer> <ContentControl Name="PART_FindToolBarHost" Grid.Row="2" /> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <DocumentViewer Name="documentViewer" Margin="10" /> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System.IO; using System.Windows; using System.Windows.Documents; using System.Windows.Xps.Packaging; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); XpsDocument xpsDocument = new XpsDocument("sample.xps", FileAccess.Read); FixedDocumentSequence sequence = xpsDocument.GetFixedDocumentSequence(); FixedDocument fixedDocument = sequence.References[0].GetDocument(false); this.documentViewer.Document = fixedDocument.DocumentPaginator.Source; } #endregion } } |