更新代码中的按钮样式



我在PhoneApplicationPage.Resources内的XAML中添加了一个名为ButtonStyle1的样式。我需要修改一些参数,这取决于应用程序的当前主题,光或暗。需要修改的参数为Background设置器和Pressed状态的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>

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

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

将背景绑定到theme属性,并使用转换器返回相应的画笔

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

相关内容

  • 没有找到相关文章

最新更新