为什么 MaterialDesignToolKit 中的自定义按钮样式没有 ContentPresenter?

Why custom Style of Button in MaterialDesignToolKit don't have a ContentPresenter?

我正在研究MaterialDesignToolKit(WPF)的源代码,现在专注于Ripple Effect。我发现 Ripple Effect 来自 Button 的自定义样式(如下所示)。但是在 Button 的 ControlTemplate 中,我没有看到任何 ContenPresenter。相反,有一个 AdornerDecorator。

据我所知,Button是ContentControl,如果你给它一个新的Template,你应该把ContentPresenter somewhere.But放在这里,它没有。

不知道为什么,求助me.Thanks。

<Style x:Key="MaterialDesignRaisedButton" TargetType="{x:Type ButtonBase}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
    <Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}" />
    <Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}" />
    <Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}" />
    <Setter Property="wpf:RippleAssist.Feedback" Value="White" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth1" />
    <Setter Property="TextBlock.FontWeight" Value="Medium" />
    <Setter Property="TextBlock.FontSize" Value="14" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Padding" Value="16,4,16,4" />
    <Setter Property="Height" Value="32" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Grid>
                    <AdornerDecorator CacheMode="{Binding RelativeSource={RelativeSource Self}, Path=(wpf:ShadowAssist.CacheMode)}">
                        <Grid>
                            <Border
                                x:Name="border"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="2"
                                Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}" />
                            <Border
                                HorizontalAlignment="Left"
                                Background="{DynamicResource MaterialDesignBackground}"
                                Opacity=".4">
                                <Border.Width>
                                    <MultiBinding Converter="{StaticResource RangeLengthConverter}">
                                        <Binding Path="(wpf:ButtonProgressAssist.Minimum)" RelativeSource="{RelativeSource TemplatedParent}" />
                                        <Binding Path="(wpf:ButtonProgressAssist.Maximum)" RelativeSource="{RelativeSource TemplatedParent}" />
                                        <Binding Path="(wpf:ButtonProgressAssist.Value)" RelativeSource="{RelativeSource TemplatedParent}" />
                                        <Binding Path="ActualWidth" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ButtonBase}}" />
                                    </MultiBinding>
                                </Border.Width>
                            </Border>
                        </Grid>
                    </AdornerDecorator>
                    <wpf:Ripple
                        Padding="{TemplateBinding Padding}"
                        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        Focusable="False"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="border" Property="wpf:ShadowAssist.Darken" Value="True" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter TargetName="border" Property="wpf:ShadowAssist.Darken" Value="True" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Opacity" Value="0.23" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

您不必在模板中包含 ContentPresenter ...您只需要提供一种显示内容的方法。内容展示者只是实现这一目标的一种方式。

在这种情况下,模板的内容 属性 绑定到 wpf:Ripple 控件,后者依次显示内容。

本质上,此控件模板将呈现内容的责任传递给另一个控件。