XAML中Button无法添加颜色边框问题求助
问题根源是你自定义的Button ControlTemplate里的<Border>元素没有绑定Button的BorderBrush和BorderThickness属性,导致你在Button上设置这两个属性时,模板根本没有使用这些值。
修改步骤:
- 在ControlTemplate的Border标签中,添加
BorderBrush="{TemplateBinding BorderBrush}"和BorderThickness="{TemplateBinding BorderThickness}"绑定 - 建议在Button的Style中默认设置
BorderThickness的值(比如1),因为默认BorderThickness可能为0,即使设置了BorderBrush也不会显示
修改后的完整Style代码:
<Style TargetType="Button"> <Setter Property="Width" Value="200"/> <Setter Property="Background" Value="#FF151515"/> <Setter Property="Foreground" Value="White"/> <Setter Property="Height" Value="50"/> <Setter Property="FontFamily" Value="Arial"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="Width" Value="500"/> <!-- 默认设置边框厚度,确保边框能显示 --> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <!-- 绑定BorderBrush和BorderThickness属性 --> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#FF2E2E2E"/> </Trigger> </Style.Triggers> </Style>
这样修改后,你在Button上设置的BorderBrush="#FFDC3B3B"就能生效,边框会正常显示。如果需要调整边框样式,也可以直接在Style或Button实例上修改这两个属性。
内容的提问来源于stack exchange,提问作者elnexreal
相关产品推荐
相关产品推荐

