【问题标题】:DropShadowPanel adapt to button template styleDropShadowPanel 适配按钮模板样式
【发布时间】:2017-04-04 19:08:19
【问题描述】:

我正在使用 UWP Toolkit 的 DropShadowPanel 对 Button 控件应用阴影效果。

这里是文档:DropShadowPanel XAML Control

事实是我为圆形边框编辑了按钮样式的模板,但 DropShadowPanel 不遵循新模板:

<controls:DropShadowPanel BlurRadius="4.0"
                              ShadowOpacity="0.70"
                              OffsetX="5.0"
                              OffsetY="5.0"
                              Color="Black"
                              HorizontalAlignment="Left"
                              Margin="91,90,0,0"
                              VerticalAlignment="Top">
        <Button x:Name="button"
                Content="Button"
                Style="{StaticResource ButtonStyle1}" />
    </controls:DropShadowPanel>

结果:

所以我期待类似的东西:

你有什么想法或导致类似的结果吗?

提前感谢您的帮助,

问候

【问题讨论】:

  • 您在 ButtonStyle1 中使用了什么使其圆润?
  • 我将 CornerRadius 属性设置为 RootGrid 的 10,然后设置 ContentPresenter。

标签: uwp uwp-xaml windows-community-toolkit


【解决方案1】:

可能有点晚了,但这里有一个Button 样式,可以为您提供圆形阴影。正如我在this answer 中解释的那样,您需要在ControlTemplate 中使用Rectangle 来获取阴影的alpha 蒙版

请注意,我必须创建自己的焦点视觉对象并关闭系统,因为后者不支持圆角。

<Style x:Key="ShadowButtonStyle"
       TargetType="Button">
    <Setter Property="Background"
            Value="#FF399C94" />
    <Setter Property="Foreground"
            Value="White" />
    <Setter Property="BorderBrush"
            Value="{ThemeResource SystemControlForegroundTransparentBrush}" />
    <Setter Property="BorderThickness"
            Value="{ThemeResource ButtonBorderThemeThickness}" />
    <Setter Property="Padding"
            Value="24,8" />
    <Setter Property="HorizontalAlignment"
            Value="Left" />
    <Setter Property="VerticalAlignment"
            Value="Center" />
    <Setter Property="FontFamily"
            Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontWeight"
            Value="Normal" />
    <Setter Property="FontSize"
            Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="UseSystemFocusVisuals"
            Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="RootGrid"
                      Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundVisual"
                                                                   Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundVisual"
                                                                   Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>

                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation Duration="0"
                                                     To="1"
                                                     Storyboard.TargetProperty="(UIElement.Opacity)"
                                                     Storyboard.TargetName="FocusVisual"
                                                     d:IsOptimized="True" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused" />
                            <VisualState x:Name="PointerFocused">
                                <Storyboard>
                                    <DoubleAnimation Duration="0"
                                                     To="1"
                                                     Storyboard.TargetProperty="(UIElement.Opacity)"
                                                     Storyboard.TargetName="FocusVisual"
                                                     d:IsOptimized="True" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <Rectangle x:Name="FocusVisual"
                               Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}"
                               StrokeThickness="2"
                               RadiusX="12"
                               RadiusY="12"
                               Margin="-2"
                               Opacity="0" />
                    <controls:DropShadowPanel HorizontalContentAlignment="Stretch"
                                              Color="Black"
                                              ShadowOpacity="0.8"
                                              OffsetY="4">
                        <Rectangle x:Name="BackgroundVisual"
                                   RadiusX="12"
                                   RadiusY="12"
                                   Fill="{TemplateBinding Background}" />
                    </controls:DropShadowPanel>
                    <ContentPresenter x:Name="ContentPresenter"
                                      Padding="{TemplateBinding Padding}"
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                      AutomationProperties.AccessibilityView="Raw"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      ContentTransitions="{TemplateBinding ContentTransitions}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

... 这就是它的样子。 :)

【讨论】:

    【解决方案2】:

    我不知道面板是如何工作的,但我会尝试将面板放在按钮模板内,就在具有实际圆角的边框周围。否则 - 我会使用 NineGrid 来实现投影。

    【讨论】:

    • 已经过测试,没有预期的结果。也许有一个简单的解决方案,我将继续搜索。感谢您的帮助。
    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多