【问题标题】:How to remove highlight of button onclick如何删除按钮onclick的突出显示
【发布时间】:2013-09-30 12:42:30
【问题描述】:

当一个按钮被点击时,它会以用户为windwos手机设备设置的主题颜色突出显示。应该调用什么方法来覆盖这种行为。我尝试使用MouseEnter并将背景设置为白色,但它不起作用,

       System.Windows.Media.SolidColorBrush whiteBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 255, 255));
        signIn.Background = whiteBrush;

有人可以帮忙吗?

【问题讨论】:

  • 感谢乐于帮助:)

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


【解决方案1】:

这是您需要进行编辑的地方。相应地更改按下的标签

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

将其添加到页面并让我知道 :)

【讨论】:

  • 是的,这就是我要找的东西.. 非常感谢.. 它有效!
【解决方案2】:

你必须使用Styles。这允许您使用 VisualStateManager 并控制您的控件在任何显示点(正常、悬停、按下、聚焦)期间的外观。 This article 在这个话题上相当冗长。

【讨论】:

    【解决方案3】:

    不要使用点击事件,只使用MouseLeftButtonUpMouseLeftButtonDown事件。

    当您按下按钮时,MouseLeftButtonDown 事件被触发,因此在此方法中,您可以使用

    更改按钮的颜色
    System.Windows.Media.SolidColorBrush whiteBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 255, 255));
            signIn.Background = whiteBrush;
    

    MouseLeftButtonUp 事件中,您可以设置正常的按钮颜色

    希望它对你有用。

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 2016-12-01
      • 2016-04-26
      相关资源
      最近更新 更多