【问题标题】:WP7 - set button Background and BorderBrush in addition to Foreground color on clickWP7 - 设置按钮背景和 BorderBrush 以及单击时的前景色
【发布时间】:2011-02-03 20:32:57
【问题描述】:

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

<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>

我希望使用这个模板来设置 BorderBrushForeground 颜色,但是我对这个 XAML 的调整只得到了不好的效果。

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

【问题讨论】:

    标签: xaml windows-phone-7 styles


    【解决方案1】:

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

    不要与 XAML 的禅宗抗争,顺其自然。将 Expression Blend 纳入所有 GUI 设计的开发工作流程,或者为手动 XAML 编辑带来的无尽恐惧做好准备。

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

    我强烈建议您花 30 分钟观看这 4 部“我该怎么做?” Steve White 的 VSM 视频@http://expression.microsoft.com/en-us/cc643423.aspx

    这 4 个视频在我从事 VSM 工作的早期帮助我了解如何使用 VSM 以及如何将我的 UI 逻辑最好地表达到视觉状态中。

    在 Expression Blend 中,通过单击更改背景颜色非常简单:

    1. 在 Expression Blend 中拖放一个新按钮。
    2. 右键单击“编辑模板 --> 编辑副本”。
    3. 从“状态”窗格中选择“按下”VSM 状态。
    4. 更改“ButtonBackground”的背景颜色。

    【讨论】:

    • 您的评论很有道理,我曾与 Blend 合作过一些。我唯一的问题是我想要做的就是调整我的按钮,以便我可以在代码隐藏中设置颜色,并且不得不使用混合来尝试这样做,就像用火箭筒杀死一只蚂蚁一样。也许这是对 WP7/Silverlight/WPF 的评论。
    • 不是我希望的解决方案,但它可能是正确的并且可能是最好的解决方案。感谢您富有洞察力的回答。
    • Blend 是画笔,Visual Studio XAML 编辑是火箭筒。在 Silverlight/WPF 中设计任何 UI 时,您应该始终使用 Blend。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2021-03-28
    相关资源
    最近更新 更多