【问题标题】:Windows Phone 7 (WP7) Change a button's background color on clickWindows Phone 7 (WP7) 单击时更改按钮的背景颜色
【发布时间】:2010-08-13 12:51:16
【问题描述】:

这似乎是一个非常非常简单的问题,但我想不通。罪魁祸首似乎是 WP7 的默认样式。单击按钮时,它将背景颜色更改为白色,然后返回按钮的默认背景。

我的问题是我想在单击按钮时更改按钮的背景。我找不到任何可能的方法来做到这一点。

我尝试在代码中设置背景,但没有任何效果。我认为它被默认样式覆盖了。

我尝试在 Blend 中使用属性更改行为,但结果完全相同。

我已经尝试为按钮创建一个新的视觉状态并在点击时设置它,但这有点错误,并且对于我正在处理的按钮数量有很大的开销。而且,它没有用。

我可以在点击事件上设置其他按钮的背景,而不是被点击的按钮。

这是一个令人讨厌的障碍!我确定这是一行代码的答案。 :)

【问题讨论】:

  • 我有同样的问题,我只是想让它透明,因为我使用图像作为背景。但是,我希望在单击按钮时没有背景颜色,因为我使用图像作为按钮背景。我怎样才能做到这一点? :)

标签: windows expression-blend windows-phone-7


【解决方案1】:

您需要做的是创建一个按钮模板来修改 Pressed 视觉状态。

在混合中,选择您的按钮,单击菜单项“对象”->“编辑模板”->“编辑副本...”并创建一个新模板。在状态窗口中,选择 CommonStates 视觉状态组中的 Pressed 视觉状态。现在在对象层次结构中选择 ButtonBackground 并在属性窗口中编辑背景画笔。

我将 Pressed 状态的背景编辑为纯青色,最终得到类似 XAML 的内容。

<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 和 Foreground。有什么想法吗?我尝试放入的所有代码都不起作用(或产生更差的结果)
  • @pearcewg +1 - 我一直在尝试这样做,但不知道要更改哪个属性。
【解决方案2】:

我认为参考实际背景,然后进行更改可能会有所帮助。这是一个将实例作为按钮的方法。

        private void HighlightButton(Button btnToHighlight)
        {

            SolidColorBrush sBrush = (SolidColorBrush)btnToHighlight.Background;


            sBrush.Color = //enter your colour here
            btnToHighlight.Background = sBrush;

        }

【讨论】:

    【解决方案3】:
    <ControlTemplate x:Key="ButtonNextOver" TargetType="Button">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00" Storyboard.TargetProperty="Background" Storyboard.TargetName="hoverbutton">
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <ImageBrush ImageSource="/NhomMua;component/Image/ico_next_over.png"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused"/>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="hoverbutton">
                            <Border.Background>
                                <ImageBrush ImageSource="/NhomMua;component/Image/ico_next.png"/>
                            </Border.Background>
                        </Border>
                    </Grid>
                </ControlTemplate>
    

    【讨论】:

      【解决方案4】:

      要在按下按钮时更改按钮背景,我使用了模板。正如马特所指出的,在 Blend 中打开项目。转到按钮 > 右键单击​​ > 编辑模板 > 编辑副本。将为您的按钮创建一个新模板并将其附加在 XAML 页面的开头附近。

      现在,由于您需要更改按下按钮时的按钮行为,因此您需要更改 VisualState。转到“按下”视觉状态并凝视它。这是“按下”的视觉状态。

      <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>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush">
                 <DiscreteObjectKeyFrame KeyTime="0" Value="#FF373737" />
            </ObjectAnimationUsingKeyFrames>
       </Storyboard>
      

      将#FF373737 的值更改为您想要的任何值。你现在已经准备好了。

      【讨论】:

        猜你喜欢
        • 2014-10-09
        • 2017-08-02
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 2014-10-07
        • 2015-03-29
        相关资源
        最近更新 更多