2015-06-14 40 views
0

我正在構建Windows Phone 8.1(非SilverLight)應用程序。我想切換時,我的ToggleButtons看起來不一樣,所以我有以下代碼:Windows Phone 8.1中每個人的ToggleButton的控制風格

<Page.Resources> 
    <!-- Custom style for Windows.UI.Xaml.Controls.Primitives.ToggleButton --> 
    <Style TargetType="ToggleButton"> 
     <Setter Property="Background" Value="{ThemeResource ToggleButtonBackgroundThemeBrush}" /> 
     <Setter Property="Foreground" Value="{ThemeResource ToggleButtonForegroundThemeBrush}"/> 
     <Setter Property="BorderBrush" Value="{ThemeResource ToggleButtonBorderThemeBrush}" /> 
     <Setter Property="BorderThickness" Value="{ThemeResource ToggleButtonBorderThemeThickness}" /> 
     <Setter Property="Padding" Value="12,4,12,5" /> 
     <Setter Property="HorizontalAlignment" Value="Left" /> 
     <Setter Property="VerticalAlignment" Value="Center" /> 
     <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" /> 
     <Setter Property="FontWeight" Value="SemiBold" /> 
     <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" /> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ToggleButton"> 
      <Grid> 
       <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup x:Name="CommonStates"> 
        <VisualState x:Name="Normal" /> 
        <VisualState x:Name="Checked"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" /> 
          </ObjectAnimationUsingKeyFrames> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderThickness"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="0" /> 
          </ObjectAnimationUsingKeyFrames> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonCheckedBorderThemeBrush}" /> 
          </ObjectAnimationUsingKeyFrames> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ToggleButtonCheckedForegroundThemeBrush}" /> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
       </VisualStateGroup> 
       </VisualStateManager.VisualStateGroups> 
       <Border x:Name="Border" 
       Background="{TemplateBinding Background}" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}" 
       Margin="3"> 
        <ContentPresenter x:Name="ContentPresenter" 
        Content="{TemplateBinding Content}" 
        ContentTransitions="{TemplateBinding ContentTransitions}" 
        ContentTemplate="{TemplateBinding ContentTemplate}" 
        Margin="{TemplateBinding Padding}" 
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
        AutomationProperties.AccessibilityView="Raw"/> 
       </Border> 
       <Rectangle x:Name="FocusVisualWhite" 
       IsHitTestVisible="False" 
       Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}" 
       StrokeEndLineCap="Square" 
       StrokeDashArray="1,1" 
       Opacity="0" 
       StrokeDashOffset="1.5" /> 
       <Rectangle x:Name="FocusVisualBlack" 
       IsHitTestVisible="False" 
       Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}" 
       StrokeEndLineCap="Square" 
       StrokeDashArray="1,1" 
       Opacity="0" 
       StrokeDashOffset="0.5" /> 
      </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    </Style> 
</Page.Resources> 

這工作,但它適用於所有ToggleButton s表示我在頁面上!正如在這裏看到:

multiple toggles

如何讓我這個選擇,使其只適用於切換按鈕左側(作爲一個例子)。

回答

2

這是非常簡單隻需添加鍵值名,以你的風格e.g

<Style TargetType="ToggleButton" x:Key="MytoggleButton"> 

現在的切換按鈕要實現這種風格只是參考的關鍵。

<ToggleButton Style="{StaticResource MytoggleButton}" ...> 
+1

我這樣做,但在我的ToggleButton上,我在做'Template =「{StaticResource KeyName}」',它不工作。一旦你知道它是如何完成的,一切都很簡單:)謝謝。 – Ciwan