2016-11-17 25 views
1

我想修改所有AppBarButton項目的ControlTemplate。是否有可能在OnLaunched()或其他地方做這樣的事情?交換控制的默認ControlTemplate

Windows.UI.Xaml.Controls.AppBarButton.TemplateProperty = Windows.UI.Xaml.Application.Current.Resources["DefaultAppBarButtonControlTemplate"] as Windows.UI.Xaml.Controls.ControlTemplate; 

上面的代碼不起作用(只讀屬性),但它應該證明,我正在嘗試做什麼。覆蓋整個樣式確實可行,但也只覆蓋ControlTemplate?我不能在我的情況下使用自定義控件。

回答

1

如果您想將樣式應用於相同類型的應用中的所有控件,只需創建一個不帶x:Key屬性的樣式,並將其放置在Application.Resources或在那裏引用的ResourceDictionary中。

作爲示例,我採用了AppBarButton的默認樣式,並更改了標籤和圖標的位置。我確實刪除了所有其他財產製定者,只更改了Template。 XAML樣式屬性按其加載順序被覆蓋:首先是系統默認值,然後是應用程序資源,然後是頁面/控件資源和內聯樣式。因此,由於我只定義了Template屬性,所有其他樣式屬性(例如Foreground)仍然是默認系統屬性。的AppBarButton

樣品XAML風格反轉文本和圖標位置:

<Application.Resources> 
    <Style TargetType="AppBarButton"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="AppBarButton"> 
        <Grid 
         x:Name="Root" 
         MinWidth="{TemplateBinding MinWidth}" 
         MaxWidth="{TemplateBinding MaxWidth}" 
         Background="{TemplateBinding Background}"> 
         <TextBlock 
          x:Name="OverflowTextLabel" 
          Margin="12,0,12,0" 
          Padding="0,5,0,7" 
          HorizontalAlignment="Stretch" 
          VerticalAlignment="Top" 
          FontFamily="{TemplateBinding FontFamily}" 
          FontSize="15" 
          Foreground="{TemplateBinding Foreground}" 
          Text="{TemplateBinding Label}" 
          TextAlignment="Left" 
          TextTrimming="Clip" 
          TextWrapping="NoWrap" 
          Visibility="Collapsed" /> 

         <StackPanel x:Name="ContentRoot" MinHeight="{ThemeResource AppBarThemeCompactHeight}"> 
          <TextBlock 
           x:Name="TextLabel" 
           Margin="0,0,0,6" 
           FontFamily="{TemplateBinding FontFamily}" 
           FontSize="12" 
           Foreground="{TemplateBinding Foreground}" 
           Text="{TemplateBinding Label}" 
           TextAlignment="Center" 
           TextWrapping="Wrap" /> 
          <ContentPresenter 
           x:Name="Content" 
           Height="20" 
           Margin="0,14,0,4" 
           HorizontalAlignment="Stretch" 
           AutomationProperties.AccessibilityView="Raw" 
           Content="{TemplateBinding Icon}" 
           Foreground="{TemplateBinding Foreground}" /> 

         </StackPanel> 


         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="ApplicationViewStates"> 
           <VisualState x:Name="FullSize" /> 
           <VisualState x:Name="Compact"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Overflow"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentRoot" Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="OverflowWithToggleButtons"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentRoot" Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Margin"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="38,0,12,0" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"> 
            <Storyboard> 
             <PointerUpThemeAnimation Storyboard.TargetName="OverflowTextLabel" /> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="PointerOver"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <PointerUpThemeAnimation Storyboard.TargetName="OverflowTextLabel" /> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Background"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <PointerDownThemeAnimation Storyboard.TargetName="OverflowTextLabel" /> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextLabel" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="OverflowTextLabel" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="InputModeStates"> 
           <VisualState x:Name="InputModeDefault" /> 
           <VisualState x:Name="TouchInputMode"> 
            <VisualState.Setters> 
             <Setter Target="OverflowTextLabel.Padding" Value="0,11,0,13" /> 
            </VisualState.Setters> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Application.Resources>