DropShadowEffect方向遵循控件的RotateTransform

时间:2019-04-11 02:53:42

标签: c# wpf xaml user-controls wpf-controls

关于我的阴影效果,我需要一些帮助。我目前正在使用带阴影的控件进行旋转。但是当我旋转控件时,下拉阴影也会跟随该方向。这些是示例图像:

this is the normal look of the dropshadow when the user control is not yet rotated.

and this is the dropshadow after rotating the control

这是我用于该控件和阴影的代码:

<local:CogWheel x:Name="CogWheel2" Width="100" Height="100" GearColor="#FF4D5D" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="105,13,0,0" RenderTransformOrigin="0.5,0.5">
                    <local:CogWheel.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform Angle="100"/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </local:CogWheel.RenderTransform>
                    <local:CogWheel.Effect>
                        <DropShadowEffect Color="#FF4D5D" BlurRadius="20" ShadowDepth="12" Direction="280" Opacity="0.70"/>
                    </local:CogWheel.Effect>
                </local:CogWheel>

我想旋转控件,但保留其阴影方向。任何帮助,将不胜感激。谢谢!

编辑:

我自己解决了此问题,方法是将控件旋转到透明网格,然后将阴影添加到网格中。

2 个答案:

答案 0 :(得分:1)

我认为您可以在“阴影效果”方向上添加“角度”以保留效果

   var rotate = (CogWheel2.RenderTransform as TransformGroup).Children.Where(x => x is RotateTransform).FirstOrDefault() as RotateTransform;
        this.shadowEffect.Direction = 280 + rotate.Angle;

答案 1 :(得分:1)

在父网格上应用阴影效果即可完成工作。

详细说明Justin CI的答案:将DropShadowEffect.Direction绑定到RotateTransform.Angle也是可行的。动画播放时很方便。

<local:CogWheel x:Name="CogWheel2" GearColor="#FF4D5D" RenderTransformOrigin="0.5,0.5">
    <local:CogWheel.RenderTransform>
        <RotateTransform Angle="100" x:Name="rotation"/>
    </local:CogWheel.RenderTransform>
    <local:CogWheel.Effect>
        <DropShadowEffect Direction="{Binding Angle, ElementName=rotation}" Color="#FF4D5D" />
    </local:CogWheel.Effect>
</local:CogWheel>