gpt4 book ai didi

c# - WPF 基于样式

转载 作者:行者123 更新时间:2023-11-30 13:51:00 25 4
gpt4 key购买 nike

嘿,我正在尝试在 WPF 中设置按钮样式。我有一个基本样式:

    <Style x:Key="buttonStyle" TargetType="{x:Type ButtonBase}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="ButtonBaseControlTemplate" TargetType="{x:Type ButtonBase}">
<Grid SnapsToDevicePixels="True">
<Border x:Name="Bd" Background="#9BC4E2" BorderBrush="Black"
BorderThickness="3,3,3,3" CornerRadius="7,7,7,7" Padding="5,5,5,5">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="Content"
ContentSource="Content"
RecognizesAccessKey="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
<-- Removed triggers here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

但我也需要将它用于切换按钮,只是为了更改 IsChecked 背景颜色。但是当我这样尝试时,我无法访问边框来设置背景:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyle}">
</Style>

<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource buttonStyle}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="White"/> <-- Need to specify target name of the border here somehow -->
</Trigger>
</Style.Triggers>
</Style>

有什么建议吗?

最佳答案

这几乎就是 TemplateBinding 的设计目的

http://devlicio.us/blogs/christopher_bennage/archive/2008/07/04/templatebinding-a-bridge-between-styles-and-templates.aspx

Border.Background 属性更改为

Background="{TemplateBinding Background}"

并将此 Setter 添加到您的 buttonStyle 样式中

<Setter Property="Background" Value="#9BC4E2" />

然后你可以像这样声明你的ToggleButton

<ToggleButton Content="ToggleButton" Background="Green" />

关于c# - WPF 基于样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5275074/

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