更新:2007 年 11 月
這個範例顯示如何擴充 DocumentViewer 控制項的預設樣式。
範例
下列可延伸標記語言 (XAML) 程式碼定義的視窗包含 DocumentViewer 控制項和伴隨的樣式。這個範例樣式使用 BasedOn 屬性來擴充預設 DocumentViewer 樣式。在這個案例中,範例樣式只會左右反轉預設工具列樣式使用的背景漸層,並將它套用至內容顯示區域的背景。
<Window x:Class="SDKSample.Window1"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<Style
x:Key="MyDVStyleExtend"
BasedOn="{StaticResource {x:Type DocumentViewer}}"
TargetType="{x:Type DocumentViewer}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0.0" Color="#CC99CCFF" />
<GradientStop Offset="1.0" Color="White" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<DocumentViewer Style="{StaticResource MyDVStyleExtend}" Name="MyDocumentViewer"/>
</Grid>
</Window>
下圖顯示如何呈現這個範例中加上樣式的 DocumentViewer (不含任何內容):
擴充預設樣式的 DocumentViewer 控制項範例。
.png)
工作備註
因為這個範例中顯示的樣式會擴充 DocumentViewer 預設樣式而不是取代預設樣式,所以仍然會顯示預設 DocumentViewer 樣式定義的使用者介面 (UI) 項目 (如工具列和內容功能表)。
樣式的套用,是透過將樣式索引鍵 (x:Key) 比對項目的 Style 屬性所參考的值。在上面顯示的範例中,樣式索引鍵是 "MyDVStyleReplace"。此索引鍵本身就是任意字串值,這個值在目前範圍中必須具唯一性。
定義為本機資源的樣式必須使用上面範例中顯示的 StaticResource 語法,以傳址 (By Reference) 方式參考為靜態資源。
樣式和 ControlTempate 使用 TargetType 指出該樣式只適用於 DocumentViewer 控制項。樣式或控制項樣板的目標型別若與要套用樣式的項目不符,則會引發 InvalidOperationException 例外狀況 (Exception)。