2011-06-03 82 views
0

後隨到這樣一個問題:How do you hide a WPF DocumentViewer's menu bars?如何更改WPF控件模板

我完全新的WPF,不知道從哪裏開始與控制模板定製。 MSDN似乎沒有什麼幫助。

我只需要隱藏主DocumentViewer工具欄,但是當我嘗試自定義XAML文件時,它無法編譯。什麼是定製步驟?

+1

你是什麼意思的「主要WPF工具欄」。你是指在「DocumentViewer」上找到的工具欄嗎? – user7116 2011-06-03 22:36:28

+0

DocumentViewer工具欄,我的錯誤。 – SharpAffair 2011-06-03 23:09:47

+0

當它編譯失敗時,你會得到什麼錯誤?您應該能夠以與您所指向的線程相同的方式進行自定義。 – Tim 2011-06-03 23:21:50

回答

2

在WPF中,一個控件模板用於將一組控件與一組構成外觀和控件行爲的元素進行「皮膚」控制。除了開始使用控制模板之外,您不必知道更多。到定製控件模板的步驟是:

  1. 查找的默認樣式ElementName(其中包括控制模板)使用MSDN,ShowMeTheTemplate,Expression Blend中或主題文件
  2. 複製默認風格融入的資源你應用,通常在App.xaml或一個元素的資源,例如一個或Window
  3. Grid
  4. 變化從x:Key="{x:Type ElementName}"x:Key="myStyleName"
  5. 密鑰修改控制模板來添加,刪除或改變的元素和屬性從默認
  6. 使用上的ElementName情況下,這種風格中加入屬性Style="{StaticResource myStyleName}"

因此,讓我們做到這一點。 Here's默認樣式爲DocumentViewer來自MSDN。我們看到一大段以<ToolBar ...>開頭,所以我們將刪除所有這些。然後,如果你按照其餘的步驟,你會最終像這樣的XAML:

<Grid> 
    <Grid.Resources> 
     <Style x:Key="documentViewerNoToolbarStyle" 
      TargetType="{x:Type DocumentViewer}"> 
      <Setter Property="Foreground" 
       Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" /> 
      <Setter Property="Background" 
       Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> 
      <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.Background> 
            <SolidColorBrush Color="{DynamicResource ControlLightColor}" /> 
           </Grid.Background> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="Auto" /> 
            <RowDefinition Height="*" /> 
            <RowDefinition Height="Auto" /> 
           </Grid.RowDefinitions> 

           <ScrollViewer Grid.Row="1" 
            CanContentScroll="true" 
            HorizontalScrollBarVisibility="Auto" 
            x:Name="PART_ContentHost" 
            IsTabStop="true"> 
            <ScrollViewer.Background> 
             <LinearGradientBrush EndPoint="0.5,1" 
              StartPoint="0.5,0"> 
              <GradientStop Color="{DynamicResource ControlLightColor}" 
               Offset="0" /> 
              <GradientStop Color="{DynamicResource ControlMediumColor}" 
               Offset="1" /> 
             </LinearGradientBrush> 
            </ScrollViewer.Background> 
           </ScrollViewer> 

           <ContentControl Grid.Row="2" 
            x:Name="PART_FindToolBarHost"/> 
          </Grid> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Grid.Resources> 
    <DocumentViewer Style="{StaticResource documentViewerNoToolbarStyle}"/> 
</Grid>