【问题标题】:UWP Button Changes Colors when Mouse hovers overUWP 按钮在鼠标悬停时更改颜色
【发布时间】:2016-12-11 05:57:34
【问题描述】:

我正在尝试创建一个 UWP 按钮,当鼠标指针悬停在它上面时,它将改变背景颜色。我遇到的麻烦是,默认情况下,它似乎已经这样做了,但不是我想要的颜色。当我将鼠标悬停在我的红色按钮上时,它会变成默认灰色,然后当我鼠标移出时又变回来。我用 C# 编写了代码,试图让它在我悬停在它上面时变成蓝色

private void button_PointerEntered_1(object sender, PointerRoutedEventArgs e)
    {
        button.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 255));
    }

    private void button_PointerExited_1(object sender, PointerRoutedEventArgs e)
    {
        button.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 255, 0, 0));
    }

下面是按钮的 XAML 代码

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="button" 
                Content="Button" 
                HorizontalAlignment="Left" 
                Margin="417,188,0,0" 
                VerticalAlignment="Top" 
                Height="230" 
                Width="461" 
                FontSize="72" 
                ManipulationMode="None" 
                PointerEntered="button_PointerEntered_1" 
                PointerExited="button_PointerExited_1">
            <Button.Foreground>
                <SolidColorBrush Color="Black"/>
            </Button.Foreground>
            <Button.Background>
                <SolidColorBrush Color="Red"/>
            </Button.Background>
        </Button>

    </Grid>

【问题讨论】:

    标签: c# uwp uwp-xaml


    【解决方案1】:

    一种可能但更复杂的方法是覆盖Button 样式:

    <Page.Resources>
        <Style TargetType="Button" x:Key="CustomButtonStyle">
            <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
            <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
            <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}" />
            <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}" />
            <Setter Property="Padding" Value="8,4,8,4" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
            <Setter Property="UseSystemFocusVisuals" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal">
                                        <Storyboard>
                                            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
    
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Orange" />
                                            </ObjectAnimationUsingKeyFrames>
    
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentPresenter x:Name="ContentPresenter" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" ContentTemplate="{TemplateBinding ContentTemplate}" Padding="{TemplateBinding Padding}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" AutomationProperties.AccessibilityView="Raw"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>
    
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="gridRoot">
    
        <Button Content="stackoverflow" Style="{StaticResource CustomButtonStyle}"/>
    </Grid>
    
    

    查看PointerOver 状态以及我如何设置Background 属性。

    【讨论】:

    • 这是更改悬停颜色的大量代码。
    • 这个代码太多了,不能简单地改变颜色,你可以改变资源字典的值
    • @Treycos 如果屏幕上有 3-4 个不同的按钮 - 你必须使用这个解决方案
    • 你的答案比我的好,但你的解决方案在 2k16 中不起作用,因为 UWP sdk 10240 没有 ButtonForegroundPointerOver 或 ButtonBackgroundPointerOver 刷子
    【解决方案2】:

    您可以将一个按钮拖到您的网格并左键单击它选择编辑项。然后您将看到样式默认为@Andrii 答案。如果您想更改鼠标悬停颜色,您可以更改代码@987654321 @

    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="The new Color" />
    

    您可以将The new Color 更改为您的颜色。

    【讨论】:

      【解决方案3】:

      2018 年更新答案

      实现它的最简单方法是覆盖按钮字典中的资源(对于您想要的主题)
      您可以更改名为 Button&lt;Property&gt;PointerOver 的资源键的值以使效果生效:

      <Button Background="Red" Foreground="Black"> <!-- These are only applied when your button is not being hovered-->
          <Button.Resources>
              <ResourceDictionary>
                  <ResourceDictionary.ThemeDictionaries>
                      <ResourceDictionary x:Key="Dark">
                          <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="Red"/>
                          <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Black"/>
                      </ResourceDictionary>
                      <ResourceDictionary x:Key="Light">
                          <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="Red"/>
                          <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Black"/>
                      </ResourceDictionary>
                  </ResourceDictionary.ThemeDictionaries>
              </ResourceDictionary>
          </Button.Resources>
      </Button>
      

      Here is an example on how this is done 在 Microsoft 官方文档中获得以下结果:

      和here is the list of resources that you can override 在一个按钮中

      【讨论】:

      • 唯一的缺点是我只想要一个按钮来覆盖悬停,而不是我的所有按钮。
      • 按原样使用此解决方案只会更改您放入的按钮的样式。如果您想将其应用于多个按钮,您将不得不使用Page 的资源和不是Button 的。对于本地更改,只需覆盖 &lt;Button.Resources&gt;
      • 有没有办法以编程方式做到这一点?我需要根据用户操作动态更改 ButtonBackgroundPointerOver 颜色。
      【解决方案4】:

      作为对@Treycos 更新答案的补充,您还可以使用Fluent XAML Theme Editor(也可在Store 上使用),它允许您定义调色板并为您生成大部分系统资源。

      【讨论】:

        猜你喜欢
        • 2012-08-16
        • 2017-11-29
        • 2018-07-20
        • 2021-11-10
        • 2016-03-20
        • 2018-05-25
        • 2012-08-21
        • 2016-02-01
        相关资源
        最近更新 更多