gpt4 book ai didi

WPF:如何拥有默认使用系统按钮背景的自定义按钮模板?

转载 作者:行者123 更新时间:2023-12-03 18:00:18 25 4
gpt4 key购买 nike

我正在创建一种更改按钮的 ControlTemplate 的样式(它实际上在右侧添加了箭头,因此该按钮看起来像下拉按钮(在 wpf 中缺少))。

在模板内部,我放置了实际按钮(因为我需要结果仍然像按钮一样 - 我只能更改 ContentTemplate,但我需要将实际内容与箭头一起显示,并将 ContentPresenter 放在 ContentTemplate 中会产生堆栈溢出 - wpf 只是将模板扩展为演示者并继续),以及箭头。

我的问题是我希望用户能够更改按钮的背景,所以我需要绑定(bind) Background="{TemplateBinding Background}" .这样,用户必须始终提供背景,否则它将为空。
为了防止这种情况,我知道我需要为背景提供默认值,所以我添加了 <Setter Property="Background" Value="default Background goes here"/>
问题是,这个 setter 的值应该是多少?我假设每个应用程序都会自动加载 wpf 的内置样式,并且我查看了 aero.normalcolor.xaml,它们提供了 ButtonNormalBackground样式,稍后将其用作默认按钮样式<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> (aero.normalcolor.xaml,第 47 行)。

正是我想要的,但是,当我使用这个 setter 时,找不到静态资源。然后我将其更改为 DynamicResource,但在这种情况下,按钮的背景保持透明 - 未使用 ButtonNormalBackground!

我应该如何提供系统的按钮外观作为默认值?谢谢!

PS:BorderBrush 的 dtto,BorderThickness - 天哪,似乎我不允许 WPF 作者一直使用的技巧:/

我的风格:

<Style TargetType="{x:Type ToggleButton}" x:Key="DropDownArrowStyle">
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="ToggleButton.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<ToggleButton
x:Name="PART_ToggleButton"
Margin="{TemplateBinding Margin}"
Padding="{TemplateBinding Padding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
IsEnabled="{TemplateBinding IsEnabled}"
IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
ContextMenu="{TemplateBinding ContextMenu}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Foreground="{TemplateBinding Foreground}"
FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
ClickMode="{TemplateBinding ClickMode}"
>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter
x:Name="ButtonContentPresenter"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
Grid.Column="0"
/>
<Border
x:Name="PART_DownArrowBorder"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent"
Focusable="False"
Grid.Column="1"
>
<Path
Focusable="False"
x:Name="PART_DownArrow"
Data="M 3,0 L 6.5,4 L 10,0"
Width="12"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Fill="Black" SnapsToDevicePixels="True"
>
</Path>
</Border>
</Grid>
</ToggleButton>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

最佳答案

您需要定义要使用的颜色并引用系统颜色:

IE。

<SolidColorBrush 
Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"
x:Key="ButtonLinkBackground"/>

或者
<Grid Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>

更多关于可用 SystemColors 的信息 How can I list colors in WPF with XAML?

关于WPF:如何拥有默认使用系统按钮背景的自定义按钮模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1848071/

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