gpt4 book ai didi

c# - 在按钮模板上设置 CornerRadius

转载 作者:IT王子 更新时间:2023-10-29 04:00:50 29 4
gpt4 key购买 nike

我想要一个没有定义 CornerRadius 的按钮和另外两个定义了的按钮,我该如何实现?

<Style TargetType="Button" x:Key="TabButton">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="0" Background="White" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style TargetType="Button" x:Key="TabButtonFirst" BasedOn="{StaticResource TabButton}">
<Setter Property="CornerRadius" Value="3,0,0,0" />
</Style>

<Style TargetType="Button" x:Key="TabButtonLast" BasedOn="{StaticResource TabButton}">
<Setter Property="CornerRadius" Value="0,0,0,3" />
</Style>

最佳答案

您不限于您正在模板化的控件的依赖属性。在这种情况下,虽然 Button 没有 CornerRadius 属性,但 Border 有,因此您可以使用 Border.CornerRadius 相反:

<Style TargetType="Button" x:Key="TabButton">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="{TemplateBinding Border.CornerRadius}"
Background="White" BorderBrush="#ccc"
BorderThickness="0,1,1,0" >
<ContentPresenter x:Name="contentPresenter"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style TargetType="Button" x:Key="TabButtonFirst" BasedOn="{StaticResource TabButton}">
<Setter Property="Border.CornerRadius" Value="3,0,0,0" />
</Style>

<Style TargetType="Button" x:Key="TabButtonLast" BasedOn="{StaticResource TabButton}">
<Setter Property="Border.CornerRadius" Value="0,0,0,3" />
</Style>

使用这种方法,您不再需要维护控件模板的多个副本。

关于c# - 在按钮模板上设置 CornerRadius,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17681022/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com