在WPF中为所有窗口中的按钮应用样式
我在XAML中有一个样式设置,用于在我的WPF窗口中创建圆角在WPF中为所有窗口中的按钮应用样式,wpf,xaml,button,styles,Wpf,Xaml,Button,Styles,我在XAML中有一个样式设置,用于在我的WPF窗口中创建圆角按钮s。我希望此样式适用于我的应用程序中所有窗口上的所有按钮 有没有一种类似于CSS的方法,我可以把它放到另一个文件中,并在我的所有窗口中以某种方式引用它?或者我每次都需要复制粘贴它。谷歌参考词典。你可以把你所有的风格都放在那里。然后,只需在窗口/用户控件中添加一个“引用” <UserControl.Resources> <ResourceDictionary> <Resou
按钮s。我希望此样式适用于我的应用程序中所有窗口上的所有按钮
有没有一种类似于CSS的方法,我可以把它放到另一个文件中,并在我的所有窗口中以某种方式引用它?或者我每次都需要复制粘贴它。谷歌参考词典。你可以把你所有的风格都放在那里。然后,只需在窗口/用户控件中添加一个“引用”
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SialTPChat.UI.Design;component/Styles/Styles.xaml" />
</ResourceDictionary>
</UserControl.Resources>
现在,上面引用的XAML文件中的所有样式都将应用于用户控件中的所有对象。您可以使用应用程序资源来实现这一点
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/SialTPChat.UI.Design;component/Styles/Styles.xaml" />
</ResourceDictionary>
</UserControl.Resources>
下面是一些代码示例(在app.xaml中)
然后,对于按钮(例如):
希望这能帮助你找到你想要的东西。如果你想以一种干净的方式完成它,你可以创建一个ResourceDictionary.xaml
,它与网页设计中的CSS
功能相同
首先,转到您的项目并添加一个ResourceDictionary
。在它里面,您可以为所有需要的元素添加样式,例如,更改应用于所有按钮的按钮的颜色背景:
// Base style for all buttons
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
</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>
请注意,这里我覆盖了绘制基本功能按钮所需的所有基本属性,如前台
,后台
,宽度
。。。和MouseOver
事件,以在鼠标经过时更改颜色。ControlTemplate
内部的Border
的CornerRadius
属性就是您要查找的半径
因此,基本上,您正在覆盖所有按钮的默认边框属性。根据我的说法,最简单的方法是:
在“设计图面”中的按钮上单击鼠标右键
选择编辑模板->编辑副本
选择“在应用程序中定义”单选按钮
样式将在App.xaml文件中创建
使用“样式”标签将该资源添加到每个按钮
如果在样式定义中省略了x:Key
,则不需要将样式应用于每个按钮。(见@sonhja的答案)谢谢,我一直在寻找那一点点!是的,该操作用于设置“所有按钮”的样式,而不仅仅是应用命名样式的按钮。想象一下,必须将Style
属性应用于您需要的每个按钮。我没有“”属性。我错过了什么?我错过了窗口。我也使用了引用,这就成功了。杰夫在下面的回答似乎非常相似,但我猜仅限于控制。
// Specific style for blue buttons
<Style TargetType="Button" x:Key="BlueButton">
<Setter Property="Background" Value="Blue" />
</Style>
<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>
<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>