我正在创建一个改变Button的ControlTemplate的样式(它实际上在右边添加了箭头,因此按钮看起来像下拉按钮(在wpf中缺失)。)
在模板内部,我放了实际按钮(因为我需要结果仍然像按钮 - 我只能更改ContentTemplate,但我需要与箭头一起显示实际内容,并将ContentPresenter放入ContentTemplate中会产生堆栈溢出 - wpf只需将模板扩展到演示者中,然后继续使用箭头。
我的问题是我希望用户能够更改按钮的背景,所以我需要绑定Background="{TemplateBinding Background}"
。有了这个,用户必须始终提供背景,否则它将为null。
为了防止这种情况,我知道我需要为后台提供默认值,因此我添加了<Setter Property="Background" Value="default Background goes here"/>
问题是,这个二传手应该是什么价值?我假设wpf的内置样式是为每个应用程序自动加载的,我调查了aero.normalcolor.xaml,它们提供了ButtonNormalBackground
样式,后来将它用作默认按钮样式<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
( aero.normalcolor.xaml,第47行。
正是我想要的,但是,当我使用这个setter时,找不到静态资源。然后我将其更改为DynamicResource,但在这种情况下,按钮的背景保持透明 - 不使用ButtonNormalBackground!
我应该如何提供系统的按钮外观作为默认值?谢谢!
PS:对于BorderBrush,BorderThickness - geez的dtto,似乎我不允许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>
答案 0 :(得分:0)
您需要定义要使用的颜色并引用系统颜色:
即
<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?