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

时间:2019-02-14 07:08:19

标签: wpf material-design controltemplate contentpresenter

我正在研究MaterialDesignToolKit(WPF)的源代码,现在着眼于涟漪效应。我发现涟漪效应来自Button的自定义样式(如下所示)。但是在Button的ControlTemplate中,我没有看到任何ContenPresenter。而是有一个AdornerDecorator。

据我所知,Button是ContentControl,如果您为其提供新模板,则应将ContentPresenter放在某个位置。但是在这里,它没有。

我不知道为什么,请帮助我。谢谢。

<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>

1 个答案:

答案 0 :(得分:1)

您不必在模板中包括ContentPresenter ...您只需要提供一种显示内容的方法即可。内容演示者只是做到这一点的一种方法。

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

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