Wpf 按钮';s bordesr不可见,没有背景色
最初我有一个按钮,工作得很好。现在我想把拐角弄圆Wpf 按钮';s bordesr不可见,没有背景色,wpf,xaml,Wpf,Xaml,最初我有一个按钮,工作得很好。现在我想把拐角弄圆 <Button Content="Start" x:Name="Start" Style="{StaticResource RoundButtonTemplate}" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="75" Click="Start_Click"> 在App.xaml中:
<Button Content="Start" x:Name="Start" Style="{StaticResource RoundButtonTemplate}"
HorizontalAlignment="Left"
Margin="20,20,0,0"
VerticalAlignment="Top"
Width="75"
Click="Start_Click">
在App.xaml中:
<Application.Resources>
<Style x:Key="RoundButtonTemplate" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="15" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
现在的问题是:
如何修改样式?查看按钮的默认样式 在您的样式中,您定义了一个具有角半径的“边框”,但无法检索颜色。您应该使用{TemplateBinding Background}添加一个Background属性,以便它绑定到按钮的Background属性(Start.Background)
始终可以复制默认样式并对其进行修改。此外,您还应该了解模板绑定的工作原理:)摆脱代码隐藏,使用默认设置器将
背景和IsEnabled
和BorderBrush
添加到样式模板中,这样您就可以完成并正确完成所有工作。它没有按预期的方式工作。@ChrisW。但我想根据不同的条件动态更改代码中的背景
和启用
。我只想把边界角弄圆,仅此而已。
<Application.Resources>
<Style x:Key="RoundButtonTemplate" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="15" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>