在 WPF 中为所有窗口中的按钮应用样式 [英] Apply style for buttons in all Windows in WPF

查看:23
本文介绍了在 WPF 中为所有窗口中的按钮应用样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XAML 中有一个样式设置,用于在 WPF 窗口中创建圆角 Button.我希望这种样式适用于我的应用程序中所有窗口上的所有按钮.

I have a style setup in my XAML for the creation of round corner Buttons in my WPF window. I would like this style to apply to all buttons on all windows in my application.

有没有办法,类似于 CSS,我可以把它放到另一个文件中,并在我所有的窗口中以某种方式引用它?还是我每次都需要复制粘贴.

Is there a way, similar to CSS, that I could put this into another file and reference it somehow in all my windows? Or do I just need to copy and paste it every time.

推荐答案

您可以使用应用程序资源来做到这一点.

You could use the application resources to do that.

例如这里有一些代码(在 app.xaml 中)

Here's a bit of code for example (in app.xaml)

<Application.Resources>
  <Style TargetType="Button" x:Key="GelButton" >
     <Setter Property="Margin" Value="1,2,1,2"/>
     <Setter Property="HorizontalAlignment" Value="Left"/>
  </Style>
</Application.Resources>

然后,对于您的按钮(例如):

and then, for your buttons (for example):

<Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 1" />
<Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 2" />

希望这能帮助您找到所需的内容.

Hope this will help you to find what you're looking for.

这篇关于在 WPF 中为所有窗口中的按钮应用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆