gpt4 book ai didi

c# - UWP 按钮在鼠标悬停时改变颜色

转载 作者:太空狗 更新时间:2023-10-29 17:32:29 31 4
gpt4 key购买 nike

我正在尝试创建一个 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>

最佳答案

2018 年更新的答案

实现它的最简单方法是覆盖按钮字典中的资源(针对您想要的主题)
您可以更改名为 Button<Property>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 官方文档中,结果如下:

enter image description here

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

关于c# - UWP 按钮在鼠标悬停时改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38778715/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com