Son*_*hja 65
如果您想以干净的方式进行,您可以创建一个ResourceDictionary.xaml与CSSWeb设计中具有相同功能的功能.
首先,转到您的项目并添加一个ResourceDictionary.在其中,您可以为所需的所有元素添加样式,例如,更改Button适用于所有按钮的颜色背景:
// Base style for all buttons
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
</Style>
Run Code Online (Sandbox Code Playgroud)
如果未在每个上指定标识符Style,则该样式将应用于与TargetType您指定的匹配的所有控件
.如果您希望按钮看起来不同,您可以执行与上面相同的操作,但也包括该样式的标识符,每个按钮将使用该标识符:
// Specific style for blue buttons
<Style TargetType="Button" x:Key="BlueButton">
<Setter Property="Background" Value="Blue" />
</Style>
Run Code Online (Sandbox Code Playgroud)
然后,在.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>
Run Code Online (Sandbox Code Playgroud)
我想这就是你要找的东西.
如果要绘制圆角按钮,则需要覆盖Template按钮的属性.这意味着你需要告诉按钮,从你覆盖它的那一刻起他需要做的每一个动作.看到这里.所以,在一个小而简化的概念中,你想写这样的东西:
<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>
Run Code Online (Sandbox Code Playgroud)
看到这里我重写得出一个基本的泛函按钮所需的所有基本属性,如Foreground,Background,Width... ...和MouseOver事件,通过鼠标悬停在上面时改变颜色.内部的CornerRadius属性是您正在寻找的半径.BorderControlTemplate
所以基本上,你要覆盖所有按钮默认出现的border属性.
小智 38
您可以使用应用程序资源来执行此操作.
以下是一些代码(在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>
Run Code Online (Sandbox Code Playgroud)
然后,对于你的按钮(例如):
<Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 1" />
<Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 2" />
Run Code Online (Sandbox Code Playgroud)
希望这能帮助您找到您想要的东西.