相对于按钮大小和彩色线条具有圆角的 WPF 按钮样式
WPF Button Style with roundend corners relative to the button size and colored line
我需要帮助制作一个如下所示的按钮:
无论按钮的大小如何,它都应该有圆角。
我目前拥有的:
App.xaml
中按钮的样式
<!-- Standard Button Colors-->
<SolidColorBrush x:Key="StandardButtonBackground" Color="#1C536F" />
<SolidColorBrush x:Key="StandardButtonForeground" Color="#FEFEFE" />
<!-- Standard Button Template-->
<Style x:Key="StandardButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource StandardButtonBackground}" />
<Setter Property="Foreground" Value="{StaticResource StandardButtonForeground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="55" Background="{StaticResource StandardButtonBackground}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在视图中使用:
<Button Style="{StaticResource StandardButton}" Content="Test" FontSize="20"/>
看起来像这样:
但是角是像素大小的,所以当按钮的大小改变时,角不会相应地改变。
接下来是应该显示状态的彩色线条。我怎样才能添加这样一行?
您可以使用转换器来获取半径和突出显示的第二个边框。
MainWindow.xaml
<Window
...
>
<Window.Resources>
<!-- Converter -->
<local:HeightToRadiusConverter x:Key="HeightToRadiusConverter"/>
<!-- Standard Button Colors-->
<SolidColorBrush x:Key="StandardButtonBackground" Color="#1C536F" />
<SolidColorBrush x:Key="StandardButtonForeground" Color="#FEFEFE" />
<SolidColorBrush x:Key="StandardButtonHighlight" Color="GreenYellow" />
<!-- Standard Button Template-->
<Style x:Key="StandardButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource StandardButtonBackground}" />
<Setter Property="Foreground" Value="{StaticResource StandardButtonForeground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource HeightToRadiusConverter}}"
Background="{TemplateBinding Background}">
<Border Margin="5" BorderThickness="2" BorderBrush="{StaticResource StandardButtonHighlight}"
CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource HeightToRadiusConverter}}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource StandardButton}" Content="Test" FontSize="20"/>
</Grid>
</Window>
转换器
public class HeightToRadiusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double height = (double)value;
return height / 2;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new NotImplementedException();
}
}
调整 window 将相应地调整按钮的大小。
我需要帮助制作一个如下所示的按钮:
无论按钮的大小如何,它都应该有圆角。
我目前拥有的:
App.xaml
中按钮的样式<!-- Standard Button Colors-->
<SolidColorBrush x:Key="StandardButtonBackground" Color="#1C536F" />
<SolidColorBrush x:Key="StandardButtonForeground" Color="#FEFEFE" />
<!-- Standard Button Template-->
<Style x:Key="StandardButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource StandardButtonBackground}" />
<Setter Property="Foreground" Value="{StaticResource StandardButtonForeground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="55" Background="{StaticResource StandardButtonBackground}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在视图中使用:
<Button Style="{StaticResource StandardButton}" Content="Test" FontSize="20"/>
看起来像这样:
但是角是像素大小的,所以当按钮的大小改变时,角不会相应地改变。
接下来是应该显示状态的彩色线条。我怎样才能添加这样一行?
您可以使用转换器来获取半径和突出显示的第二个边框。
MainWindow.xaml
<Window
...
>
<Window.Resources>
<!-- Converter -->
<local:HeightToRadiusConverter x:Key="HeightToRadiusConverter"/>
<!-- Standard Button Colors-->
<SolidColorBrush x:Key="StandardButtonBackground" Color="#1C536F" />
<SolidColorBrush x:Key="StandardButtonForeground" Color="#FEFEFE" />
<SolidColorBrush x:Key="StandardButtonHighlight" Color="GreenYellow" />
<!-- Standard Button Template-->
<Style x:Key="StandardButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource StandardButtonBackground}" />
<Setter Property="Foreground" Value="{StaticResource StandardButtonForeground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource HeightToRadiusConverter}}"
Background="{TemplateBinding Background}">
<Border Margin="5" BorderThickness="2" BorderBrush="{StaticResource StandardButtonHighlight}"
CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource HeightToRadiusConverter}}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource StandardButton}" Content="Test" FontSize="20"/>
</Grid>
</Window>
转换器
public class HeightToRadiusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double height = (double)value;
return height / 2;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new NotImplementedException();
}
}
调整 window 将相应地调整按钮的大小。