Example #1
0
    public object?Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values?.Length != 4)
        {
            return(Binding.DoNothing);
        }

        if (values[0] is not double ||
            values[1] is not double ||
            values[2] is not ShadowDepth ||
            values[3] is not ShadowEdges)
        {
            return(Binding.DoNothing);
        }

        double width  = (double)values[0];
        double height = (double)values[1];

        if (double.IsNaN(width) || double.IsInfinity(width) || double.IsNaN(height) || double.IsInfinity(height))
        {
            return(Binding.DoNothing);
        }

        DropShadowEffect?dropShadow = ShadowInfo.GetDropShadow((ShadowDepth)values[2]);

        if (dropShadow is null)
        {
            return(null);
        }

        ShadowEdges edges      = (ShadowEdges)values[3];
        double      blurRadius = dropShadow.BlurRadius;

        var rect = new Rect(0, 0, width, height);

        if (edges.HasFlag(ShadowEdges.Left))
        {
            rect = new Rect(rect.X - blurRadius, rect.Y, rect.Width + blurRadius, rect.Height);
        }

        if (edges.HasFlag(ShadowEdges.Top))
        {
            rect = new Rect(rect.X, rect.Y - blurRadius, rect.Width, rect.Height + blurRadius);
        }

        if (edges.HasFlag(ShadowEdges.Right))
        {
            rect = new Rect(rect.X, rect.Y, rect.Width + blurRadius, rect.Height);
        }

        if (edges.HasFlag(ShadowEdges.Bottom))
        {
            rect = new Rect(rect.X, rect.Y, rect.Width, rect.Height + blurRadius);
        }

        var size = new GeometryDrawing(new SolidColorBrush(Colors.White), new Pen(), new RectangleGeometry(rect));

        return(new DrawingBrush(size)
        {
            Stretch = Stretch.None,
            TileMode = TileMode.None,
            Viewport = rect,
            ViewportUnits = BrushMappingMode.Absolute,
            Viewbox = rect,
            ViewboxUnits = BrushMappingMode.Absolute
        });
    }
Example #2
0
 public override DropShadowEffect?ConvertValue(ShadowDepth value, Type targetType, object parameter, CultureInfo culture)
 {
     return(Clone(ShadowInfo.GetDropShadow(value)));
 }