在通用Windows平台中更改按钮样式

8

我尝试制作一个简单的C# UWP应用程序,但我不知道如何在鼠标悬停在按钮上时去除灰色背景。

我该怎么做?

(请记住:这是Windows 10平台的UWP,而不是Windows Phone 8.1或WPF)

The simple button

the button when my mouse is over


你需要编辑样式源码 - 附加了一个故事板,它将颜色更改为灰色 - 你可能想将该颜色更改为透明。我很快会给你详细的答案和截图。 - Matthias Herrmann
谢谢!我是个新手,不知道怎么做。 - Andrei Paciurca
请上传您按钮背后的 XAML 代码。我看不出您是否在使用 Appbarbutton 还是“普通”按钮。我猜它是“普通”的,对吗? - Matthias Herrmann
是的,它是一个普通按钮。 - Andrei Paciurca
<Button x:Name="button" Content="" HorizontalAlignment="Left" Margin="124,201,0,0" VerticalAlignment="Top" Height="150" Width="169" Foreground="#00000000" BorderBrush="Transparent" BorderThickness="0"> <Button.Background> <ImageBrush ImageSource="ms-appx:///512.png" Stretch="Fill"/> </Button.Background> </Button> - Andrei Paciurca
1个回答

15

请按照以下步骤进行操作:

  1. 在“解决方案资源管理器”中右键单击并添加一个新项目,其类型为“ResourceDictionary”。
  2. 复制按钮的默认样式,您可以在此网页上找到它,需要向下滚动一小段距离: Msdn

然后将其插入到您的 ResourceDictionary.xaml 中,格式应如下所示:

<ResourceDictionary><Style></Style></ResourceDictionary>

3. 给样式添加一个类名,像这样:

<Style x:Key="MyCustomButton"></Style>

4. 前往 App.xaml,通过添加 Resource Dictionary 来编辑它:

<Application.Resources>
    <ResourceDictionary Source="Resources.xaml"></ResourceDictionary>
</Application.Resources>

资源字典的来源是您的资源字典文件的名称。

  1. 然后像这样将样式添加到您的按钮中:<Button Style="{StaticResource MyCustomButton}"></Button>
  2. 最后回到您的 ResourceDictionary,删除在屏幕截图中看到的以下代码行,或者像我一样将其注释掉: Edit your style

如果您使用 Blend for Visual Studio,则有更简单的解决方案,您可以更快地编辑此内容,但为了保持结构并进行学习,上面的解决方案是更好的选择。


他刚才说“quicklier”了吗?是的,他说了!+1 - RareNCool

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接