2014-06-21 3 views
0

내 솔루션에 ButtonStyle1이라는 스타일을 XAML의 PhoneApplicationPage.Resources 내에 추가했습니다. 애플리케이션의 현재 테마 (밝거나 어둡습니다)에 따라 몇 가지 매개 변수를 수정할 필요가 있습니다. 변경 될 매개 변수는 Background 설정자이고 상태의 Background입니다.코드의 업데이트 단추 스타일

나는 응용 프로그램이있을 수있는 세 가지 상태가 있습니다. 표준, 밝음, 어둡게. 표준은 세터와 눌려진 상태에 대한 투명한 배경을 가지며, 빛은 Color.FromArgb(153, 00, 00, 00)이고, 어두움은 Color.FromArgb(153, 64, 76, 87)입니다.

XAML

<Style x:Key="ButtonStyle1" TargetType="Button"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> 
     <Setter Property="Padding" Value="10,5,10,6"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" Background="{TemplateBinding Background}" CornerRadius="0" Margin="0"> 
          <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="0" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

I가 내가 뒤에 따라 코드에서 이러한 매개 변수를 조정할 수 있습니다 어떻게 OnNavigatedTo

XAML.CS

private void UpdateButtonStyle() 
    { 
     var style = Application.Current.Resources["ButtonStyle1"] as Style; 


     if (Settings.TransparentMode.Value == false) //Standard Theme 
     { 
      //setter background and pressed background state will be Transparent 
     } 
     else 
     { 
      if (Settings.LightTheme.Value == false) //Dark theme 
      { 
       //setter background and pressed background state will be Color.FromArgb(153, 00, 00, 00) 
      } 
      else //Light theme 
      { 
       //setter background and pressed background state will be Color.FromArgb(153, 64, 76, 87) 
      } 
     } 
    } 

에서 호출되는 다음과 같은 방법 UpdateButtonStyle()?

답변

0

배경을 테마 속성에 바인딩하고 변환기를 사용하여 적절한 브러시를 반환하십시오.

public class ThemeToBackgroundConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     var theme = (Theme)value; 
     switch (theme.Value) 
     { 
      case Theme.Standard: 
       return new SolidColorBrush(Colors.Transparent); 
      case Theme.Dark: 
       return new SolidColorBrush(153, 64, 76, 87); 
      case Theme.Light: 
       return new SolidColorBrush(153, 00, 00, 00); 
     }   
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

XAML

<VisualState x:Name="Pressed"> 
    <Storyboard> 
     <ObjectAnimationUsingKeyFrames 
      Storyboard.TargetProperty="Foreground" 
      Storyboard.TargetName="ContentContainer"> 
      <DiscreteObjectKeyFrame 
       KeyTime="0" 
       Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/> 
     </ObjectAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames 
      Storyboard.TargetProperty="Background" 
      Storyboard.TargetName="ButtonBackground"> 
      <DiscreteObjectKeyFrame 
       KeyTime="0" 
       Value="{Binding DataContext.ThemeProperty, 
           ElementName=yourPhoneApplicationPageName, 
           Converter={StaticResource ThemeToBackgroundConverter}}"/> 
     </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 
</VisualState> 

<Grid Background="{Binding ThemeProperty, Converter={StaticResource ThemeToBackgroundConverter}}"/>