【问题标题】:How to Change Button Foreground and Background When Pressed and Released按下和释放时如何更改按钮的前景和背景
【发布时间】:2013-10-04 00:09:23
【问题描述】:

目前我需要在我的应用程序中放置一个“返回”按钮。这不是要替换硬件后退按钮,而是如果用户不希望继续他或她对项目的更改,则返回项目的先前状态。目前,我的 MainPage 有以下 xaml,它绑定图像并将“后退”按钮也放在视图上。

MainPage.xaml

<Grid x:Name="MainPageGrid" Margin="{Binding}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width=".2*"/>
                    <ColumnDefinition Width=".2*"/>
                    <ColumnDefinition Width=".2*"/>
                    <ColumnDefinition Width=".2*"/>
                    <ColumnDefinition Width=".2*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height=".2*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Image x:Name="currentPhoto" Grid.ColumnSpan="5" Grid.RowSpan="2"
                       Source="{Binding Source}" Margin="12,0,12,12" 
                       HorizontalAlignment="Center" VerticalAlignment="Center"
                       toolkit:TiltEffect.IsTiltEnabled="True"/>
                <Button x:Name="photoRefreshButton" Grid.Row="0" Grid.Column="5" 
                        BorderBrush="Transparent"  Click="photoRefreshButton_Click"
                        toolkit:TiltEffect.IsTiltEnabled="True">
                    <Image Source="Assets/Buttons/back.png"/>
                </Button>
            </Grid>

我希望能够在按下和释放按钮时切换前景和背景,但我不确定如何为此更改按钮模板样式。我想使用以下设置:

Not Pressed
Foreground = #FF1BA1E2 (ARGB: 255, 27, 161, 226)
background = transparent

Pressed 
Foreground = Current theme foreground brush
background = transparent

编辑

尝试这个对我不起作用

<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 PhoneFontSizeMediumLarge}"/>
        <Setter Property="Padding" Value="10,3,10,5"/>
        <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 PhoneForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" 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="#FF1BA1E2 "/>
                                        </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="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【问题讨论】:

    标签: c# windows-phone-7 button windows-phone-8


    【解决方案1】:

    第一件事是 NotPressed 状态只使用默认颜色而不是 Disable 状态。此外,如果您更改前景,它将更改某些文本的前景,但不会更改图像。更容易更改图像的“前景”可能是将其放在 Rectangle 内并使用不透明蒙版:

    <Style x:Key="BackButtonStyle1" 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 PhoneFontSizeMediumLarge}"/>
    
            <Setter Property="Padding" Value="10,3,10,5"/>
            <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="Stroke" Storyboard.TargetName="circleBorder">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="rectangle">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="circleBorder">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="rectangle">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            </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="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                                <Grid >
                                <Rectangle x:Name="rectangle" Fill="#FF1BA1E2">
                                    <Rectangle.OpacityMask>
                                        <ImageBrush ImageSource="Assets/Buttons/back.png"/>
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                                 <Ellipse x:Name="circleBorder" Stroke="Red"  StrokeThickness="5"/>
                                   </Grid>
    
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    【讨论】:

    • 快速提问,关于Disabled VisualState,您的意思是完全删除第一个ObjectAnimationUsingKeyFrames&gt; 吗?另外,如果我要在按钮周围放置一个圆形边框以模仿默认的应用程序栏按钮方案,您是否建议创建一个带有后退按钮和圆圈作为一个图像的单个图像,或者手动在按钮周围放置一个边框,以及如何?
    • 添加回来,以便您的按钮在禁用时更改前景。还添加了一个椭圆来显示圆形“边框”。
    【解决方案2】:

    您尝试从 XAML 执行此操作是否有任何特殊原因? IMO 从代码隐藏中更容易做到这一点。只需将这些经过编辑以反映您想要的实际前景/背景的行添加到按钮的 Clicked 事件处理程序:

    var btn = sender as Button;
    if (btn != null)
    {
        btn.Foreground=.......;
        btn.Background=.......;
    }
    

    【讨论】:

    • 我拥有的唯一按钮事件处理程序是Clicked 事件。以上将如何反映在按下和禁用状态中?我只是想在 xaml 中做这件事,因为以前有修改过这样的模板的经验,它似乎在没有代码的情况下运行良好。
    • 对不起,我想说Clicked 事件,但忘了在我的回答中写下。但是,如果您想在每次点击时在状态之间切换,同样简单: if (ForeGround==OriginalState) ForeGround=ModifiedState else ForeGround=OriginalState
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2013-10-15
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2016-12-11
    • 2022-01-16
    相关资源
    最近更新 更多