在WPF中的所有窗口中应用按钮样式
我在我的XAML中创建了一个样式设置,用于在我的WPF窗口中创建圆角Button
。我希望这种风格适用于我的应用程序中所有窗口上的所有按钮。在WPF中的所有窗口中应用按钮样式
有没有一种方法,类似于CSS,我可以把它放到另一个文件中,并在我的所有窗口中以某种方式引用它?或者我只需要每次复制和粘贴它。
您可以使用应用程序资源来做到这一点。
这里例如一些代码(在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>
,然后,为您的按钮(例如):
<Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 1" />
<Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 2" />
希望这将帮助你找到什么您正在寻找。
Google Reference Dictionary。你可以把你所有的风格放在那里。那么,只需在窗口/用户控件中添加一个“引用”即可。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SialTPChat.UI.Design;component/Styles/Styles.xaml" />
</ResourceDictionary>
</UserControl.Resources>
现在,上面引用的XAML文件中的所有样式将应用于用户控件中的所有对象。
如果你想以一种干净的方式做到这一点,你可以创建一个ResourceDictionary.xaml
,它与网页设计中的CSS
具有相同的功能。
首先,转到您的项目并添加ResourceDictionary
。它里面可以添加样式你想要的所有所需的元素,例如,适用于所有按钮的Button
的变化背景颜色:
// Base style for all buttons
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
</Style>
如果不指定每个Style
的标识符,即样式将适用于与您指定的 TargetType
匹配的所有控件。如果你想按钮看起来不同,你可以做上面一样,还包括一个标识符,以这种风格,将由每个不同的按钮可用于:
// Specific style for blue buttons
<Style TargetType="Button" x:Key="BlueButton">
<Setter Property="Background" Value="Blue" />
</Style>
然后,在你想要的风格各.xaml
要应用你必须引用到这个ResourceDictionary.xaml
正在创建:
<Window.... >
<Window.References>
<ResourceDictionary>
<ResourceDictionary Source="MyResourceDictionary.xaml" />
</ResourceDictionary>
</Window.References>
<Grid>
<Button Content="Button with red background" />
<Button Style="{StaticResource BlueButton}" Content="Button with blue background" />
</Grid>
</Window>
我认为这是你在找什么。
如果要绘制一个圆角按钮,则需要覆盖该按钮的Template
属性。这意味着你需要告诉按钮从你覆盖它的那一刻起他需要做的每一个动作。请参阅here。因此,在一个小,减少概念,你会喜欢写这样的事:
<Style TargetType="Button">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="DarkBlue" />
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="35" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}"
BorderBrush="LightBlue" BorderThickness="1" CornerRadius="15,0,15,0" x:Name="bd">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bd" Property="Background" Value="LightGray"/>
<Setter Property="Foreground" Value="White" />
<Setter Property="Cursor" Value="Hand" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
看到,这里我重写得出一个基本的泛函按钮所需的所有基本属性,如Foreground
,Background
,Width
...和MouseOver
事件,以在鼠标移过它时改变颜色。 ControlTemplate
内部的Border
的CornerRadius
属性是您正在查找的半径。
所以基本上,你重写默认为所有按钮来的边界属性。
+1用于包含所有按钮的基础样式。 – 2013-05-14 09:22:39
我没有“
我错过了'Window.References',我用[Funk's Answer here](https://stackoverflow.com/questions/40067621/each-dictionary-entry-must-have-an-associated-key-error - 消息),并做了伎俩。杰夫的答案似乎非常相似,但我猜测仅限于控制。 – Wubbler 2018-03-01 23:18:13
根据我的最简单的方法是:
右键点击一个按钮的设计表面
选择编辑模板 - >编辑复制
选择“定义中的应用“单选按钮
样式将在App.xaml文件中创建
该资源添加到每个按钮使用“风格”标签
如果省略了'X:Key'的样式定义,那么你就不需要样式应用到每个按钮。 (请参阅@ sonhja的答案。) – 2013-05-14 09:23:37
谢谢,我正在寻找那个小小的一点! – 2013-09-30 13:42:35
是的,OP有权设计“所有按钮”的样式,不仅仅是您应用指定样式的按钮。想象一下,必须将“Style”属性应用于您需要的每个按钮。 – ProfK 2016-11-19 09:48:32