WP7 - 除了点击前景颜色外,还设置按钮背景和BorderBrush

时间:2011-02-03 20:32:58

标签: xaml windows-phone-7 styles

这里有一些问题涉及在点击时设置按钮背景颜色。 这些问题用作解决方案:

<phone:PhoneApplicationPage ...>
    <phone:PhoneApplicationPage.Resources>
        <Style x:Key="ButtonStyle1" TargetType="Button">
            <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 PhoneBackgroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ColorAnimation Duration="0" To="Cyan" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>
                                        </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>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}" Background="Black">
                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Button Content="Button" Style="{StaticResource ButtonStyle1}"/>
    </Grid>
</phone:PhoneApplicationPage>

我希望使用此模板来设置 BorderBrush 前景颜色,但我调整此XAML只会产生不良影响。

[注意:行为是当我在代码隐藏中设置颜色时,它们在我的应用程序运行时不会生效,因为颜色会被样式覆盖。]

2 个答案:

答案 0 :(得分:3)

如果你手动调整XAML - 你做错了。

不要与XAML的禅宗作斗争,随之流动。将Expression Blend包含在所有GUI设计的开发工作流程中,或者为手动XAML编辑的无尽恐怖做好准备。

特别是对于VisualStateManagerm,手动编辑XAML绝对没有意义,因为它是由Silverlight团队设计的,因此可以从Expression Blend中进行最佳使用。

我强烈建议你花30分钟看这4个“我怎么样?”史蒂夫怀特@ http://expression.microsoft.com/en-us/cc643423.aspx

的VSM视频

这些4个视频在我开始使用VSM的早期帮助我了解如何使用VSM以及如何最好地将我的UI逻辑表达为Visual状态。

在Expression Blend中,在Click上更改背景颜色就像:

一样简单
  1. 拖动&amp;在Expression Blend中删除一个新按钮。
  2. 右键单击并“编辑模板 - &gt;编辑副本”。
  3. 从“状态”窗格中选择“按下”VSM状态。
  4. 更改“ButtonBackground”的背景颜色。

答案 1 :(得分:1)

相关问题