简体   繁体   English

WP7-设置按钮“背景”和“边框画笔”以及“前景颜色”

[英]WP7 - set button Background and BorderBrush in addition to Foreground color on click

There are a few questions out here which involve settings a Button background color on click. 这里有一些问题,涉及点击时设置按钮背景颜色。 Those questions used this as the solution: 这些问题将其用作解决方案:

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

I'm looking to use this template to also set the BorderBrush and Foreground colors, but my tweaking this XAML has only ended up with bad effects. 我希望使用此模板来设置BorderBrushForeground颜色,但是我对该XAML进行的调整仅以不良影响而告终。

[Note: the behavior is that when I set the colors in codebehind, they don't take effect when my app is run, because the colors are overridden by the style.] [注意:行为是当我在代码隐藏区中设置颜色时,它们在我的应用程序运行时不会生效,因为这些颜色已被样式覆盖。

If you're tweaking XAML manually - You're doing it wrong. 如果您要手动调整XAML,那么您做错了。

Don't fight the Zen of XAML, flow with it. 不要与XAML的Zen对抗,顺其自然。 Embrace Expression Blend into your development workflow for all GUI design, or be prepared for the untold horrors of manual XAML editing. 在所有GUI设计中将Expression Blend融入您的开发工作流程中,或者为手动XAML编辑带来的无尽恐惧做好准备。

Specifically for VisualStateManagerm manually editing XAML makes absolutely no sense as it was designed by the Silverlight Team so it could be optimally used from Expression Blend. 专门针对VisualStateManagerm手动编辑XAML完全没有意义,因为它是由Silverlight团队设计的,因此可以从Expression Blend中最佳使用它。

I strongly suggest you spend 30 minutes watching these 4 "How Do I?" 我强烈建议您花30分钟观看这4个“我如何?” VSM videos by Steve White @ http://expression.microsoft.com/en-us/cc643423.aspx 史蒂夫·怀特(Steve White)的VSM视频@ http://expression.microsoft.com/en-us/cc643423.aspx

These 4 videos helped me a lot in the early days of working on VSM to understand how to use VSM and how to best articulate my UI logic into Visual States. 在学习VSM的初期,这4个视频为我提供了很多帮助,帮助他们了解如何使用VSM,以及如何将UI逻辑最好地表达为可视状态。

In Expression Blend getting the background colour to change on Click is as simple as: 在Expression Blend中,获取要在Click上更改的背景颜色非常简单:

  1. Drag & drop a new button in Expression Blend. 在Expression Blend中拖放一个新按钮。
  2. Right click and "Edit Template --> Edit Copy". 右键单击,然后单击“编辑模板->编辑副本”。
  3. Choose the "Pressed" VSM state from the "States" pane. 从“状态”窗格中选择“已按下” VSM状态。
  4. Change the background colour of "ButtonBackground". 更改“ ButtonBackground”的背景颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM