【问题标题】:Update Button Style in Code Behind更新代码隐藏中的按钮样式
【发布时间】:2014-08-12 06:36:27
【问题描述】:

我在PhoneApplicationPage.Resources 内的XAML 解决方案中添加了一个名为ButtonStyle1 的样式。我需要根据应用程序的当前主题(亮或暗)修改几个参数。需要修改的参数是Background setter和Pressed状态下的Background

我有应用程序可能处于的三个状态;标准、浅色和深色。对于 setter 和 press 状态,Standard 将具有透明背景,而 Light 将为 Color.FromArgb(153, 00, 00, 00),Dark 将为 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>

我有以下方法UpdateButtonStyle()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)
            }
        }
    }

我如何在后面的代码中相应地调整这些参数?

【问题讨论】:

    标签: c# xaml button windows-phone-8 styles


    【解决方案1】:

    将背景绑定到主题属性并使用转换器返回适当的画笔。

    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}}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 2012-08-19
      • 2017-05-13
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多