【发布时间】:2020-07-07 21:34:25
【问题描述】:
我有Buttons 的样式,现在我想将其应用于RadioButtons,除了普通的Button 有一个CornerRadius,它是一个数字(每个角都相同)我想用不同的圆角半径渲染RadioButtons,像这样:
我为Button 使用自定义ControlTemplate 以及样式:
<ControlTemplate TargetType="{x:Type ButtonBase}" x:Key="ButtonTemplate">
<Border Background="{TemplateBinding Background}"
Width="{TemplateBinding Width}"
CornerRadius="22.5"
BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding Foreground}">
<TextBlock FontSize="14" FontWeight="Bold"
TextWrapping="Wrap" TextAlignment="Center"
VerticalAlignment="Center" HorizontalAlignment="Center" Margin="30 0"
Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border>
</ControlTemplate>
<Style TargetType="{x:Type ButtonBase}" x:Key="MainButton">
<Setter Property="Foreground" Value="{StaticResource IntBlue2}"/>
<Setter Property="TextBlock.Foreground" Value="{StaticResource IntBlue2}"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="MinWidth" Value="132"/>
<Setter Property="Template" Value="{StaticResource ButtonTemplate}"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="LightSlateGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="#66CDFF"/>
</Trigger>
</Style.Triggers>
</Style>
我有一个RadioButton 的样式,它使用Button 模板
<Style TargetType="RadioButton">
<Setter Property="Template" Value="{StaticResource ButtonTemplate}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource ForegroundPureWhite}"/>
<Setter Property="BorderThickness" Value="2"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledGrey}"/>
</Trigger>
</Style.Triggers>
</Style>
上述一切都有效,直到我想要每个 RadioButton 的自定义 CornerRadius。
我在ControlTemplate 中的Border 中尝试了CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}",在MainButton 样式中尝试了<Setter Property="Border.CornerRadius" Value="22.5"/>,但它不起作用(它不应用半径)。
将CornerRadius="{TemplateBinding CornerRadius}" 绑定到模板中的边框甚至无法编译,并且通过控制标签或样式中的设置器在Button 或RadioButton 上设置CornerRadius,也不会编译。
如何使用一个模板,但在每种样式或实例中使用不同的CornerRadius?
【问题讨论】:
-
这能回答你的问题吗? Set CornerRadius on button template
标签: wpf