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

5
我正在创建一个样式,用于更改Button的ControlTemplate(实际上在右侧添加箭头,使按钮看起来像下拉按钮(在wpf中缺少))。
在模板内,我放置了实际的按钮(因为我需要结果仍然像按钮 - 我可以只更改ContentTemplate,但我需要同时显示实际内容和箭头,并在ContentTemplate中放置ContentPresenter会导致堆栈溢出 - wpf将模板扩展到Presenter并继续执行),还有箭头。
我的问题是,我希望用户能够更改按钮的背景,因此我需要绑定Background =“{TemplateBinding Background}”。使用此选项,用户必须始终提供背景,否则它将为null。为了防止这种情况,我理解我需要为背景提供默认值,因此我添加了 问题是,这个setter应该是什么值?我假设wpf的内置样式会自动加载到每个应用程序中,并查看aero.normalcolor.xaml,他们提供ButtonNormalBackground样式,并将其用作默认按钮样式(aero.normalcolor.xaml,第47行)。
正是我想要的,但是当我使用此setter时,找不到静态资源。然后我将其更改为DynamicResource,但在这种情况下,按钮的背景保持透明 - 没有使用ButtonNormalBackground!
我应该如何提供系统的按钮外观作为默认值?谢谢!
PS:dtto用于BorderBrush,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>

相关链接:https://dev59.com/7XRB5IYBdhLWcg3wa2q2 - Eduardo Molteni
1个回答

0

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

例如:

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

或者

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

有关SystemColors的更多信息可在如何使用XAML列出WPF中的颜色?中找到。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接