更新 : 2007 年 11 月
DocumentViewer コントロールの既定のスタイルを置き換える方法を次の例に示します。
使用例
次の Extensible Application Markup Language (XAML) コードでは DocumentViewer コントロールとそれに合ったスタイルを持つウィンドウを定義しています。この例のスタイルは、ControlTemplate を使用して既定の DocumentViewer スタイルを置き換えます。この例のスタイルは、幅が 10 ピクセルの Border を定義しているだけであり、Border と、コンテンツを表示するために DocumentViewer が使用する内部 ScrollViewer の背景に、グラデーションの色を適用しています。
<Window x:Class="SDKSample.Window1"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Title="DocumentViewer_ExtendStyle">
<Window.Resources>
<Style
x:Key="MyDVStyleReplace"
TargetType="{x:Type DocumentViewer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DocumentViewer}">
<Grid>
<Border BorderThickness="10">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.BorderBrush>
<ScrollViewer Name="PART_ContentHost">
<ScrollViewer.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Green" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</ScrollViewer.Background>
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<DocumentViewer Style="{StaticResource MyDVStyleReplace}" Name="MyDocumentViewer"/>
</Grid>
</Window>
タスクの解説
この例で示すスタイルは、DocumentViewer の既定のスタイルを拡張するのではなく置き換えるので、DocumentViewer のツール バー、コンテキスト メニュー、およびその他のユーザー インターフェイス (UI) 要素 (既定の DocumentViewer スタイルで定義されているもの) は表示されません。
DocumentViewer コントロールのスタイルを置き換えるときは、Name が "PART_ContentHost" である ScrollViewer を、コントロール テンプレートが含んでいる必要があります。
スタイルは、要素の Style 属性で参照される値とスタイル キー (x:Key) を照合して適用します。前述の例では、スタイル キーは "MyDVStyleReplace" です。キー自体は任意の文字列値で、現在のスコープ内で一意でなければなりません。
ローカル リソースとして定義されたスタイルは、上記の例で示されているように、StaticResource 構文を使用し、静的リソースとして参照する必要があります。
スタイルおよび ControlTempate は、TargetType を使用して、このスタイルが DocumentViewer コントロールにのみ適用可能であることを示します。スタイル テンプレートまたはコントロール テンプレートと、スタイルが適用される要素との間で、ターゲットの型が一致していないと、InvalidOperationException 例外が発生します。
参照
処理手順
方法 : DocumentViewer のスタイルを拡張する