2016-11-17 18 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)はすべてデフォルトのシステムプロパティのままです。

<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> 
AppBarButton

サンプルXAMLスタイルは、テキストとアイコンの位置を反転させます