wpfmaterial-designcontroltemplatecontentpresenter

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


I'm studying source code of MaterialDesignToolKit(WPF),and now focosing on Ripple Effect. I found that Ripple Effect is come from Button's custom style(as you can see below). But in Button's ControlTemplate,I don't see any ContenPresenter. Instead,there's a AdornerDecorator.

As I know,Button is ContentControl,if you provide it a new Template,you should put ContentPresenter somewhere.But here,it didn't.

I don't know why, Please help 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>

Solution

  • You don't have to include a ContentPresenter in the template ... You just need to provide a means to display the content. A content presenter is just one way to do that.

    In this case, the template's content property is bound to the wpf:Ripple control, which in turn presents the content.

    Essentially, this control template is passing the responsibility of presenting the content to another control.