DragVisual: White Background

2 posts, 0 answers
  1. Steven
    Steven avatar
    9 posts
    Member since:
    Jan 2012

    Posted 27 Nov 2012 Link to this post

    I am using the ListBoxDragDropBehavior and would like to use the ScreenshotDragVisualProvider with the behavior, however - the DragVisual has a white background that I can't seem to change anywhere.

    Is it possible to change the white background of the DragVisual used by the ListBoxDragDropBehvior?

    I have tried the following with no luck;
    Created a default ListBoxDragVisual Style with all color properties set to transparent.
    Created an attached behavior which would listen for DragInitialize and set the DragVisual to a ContentControl with a transparent background.

    I have attached a screenshot to show the problem.

    Thanks

  2. Steven
    Steven avatar
    9 posts
    Member since:
    Jan 2012

    Posted 29 Nov 2012 Link to this post

    We've been able to come up with a workaround for the problem. We find the current background and set it on the DragVisual stackpanel.

    Here's our implementation of the ScreenshotDragVisualProvider;

    public class ScreenshotDragVisualProvider : IDragVisualProvider
    {
        public FrameworkElement CreateDragVisual(DragVisualProviderState state)
        {
            var visual = new StackPanel();
            var baseElement = state.DraggedItemContainers.OfType<FrameworkElement>().FirstOrDefault();
     
            foreach (var element in baseElement.Ancestors<FrameworkElement>().OfType<FrameworkElement>())
            {
                Brush brush = null;
     
                if (element is Panel)
                    brush = (element as Panel).Background;
                else if (element is Border)
                    brush = (element as Border).Background;
                 
                if (brush == null)
                    continue;
     
                var solidColorBrush = brush as SolidColorBrush;
     
                if (solidColorBrush != null && solidColorBrush.Color == Colors.Transparent)
                    continue;
     
                visual.Background = brush;
                break;
            }
     
            foreach (var rect in state.DraggedItemContainers.OfType<FrameworkElement>().Select<FrameworkElement, Rectangle>(CreateRectangle))
            {
                visual.Children.Add(rect);
            }
     
            return visual;
        }
     
        private static Rectangle CreateRectangle(FrameworkElement container)
        {
            var brush = new ImageBrush {ImageSource = new WriteableBitmap(container, null)};
            var rectangle = new Rectangle
            {
                Width = container.RenderSize.Width,
                Height = container.RenderSize.Height,
                Fill = brush
            };
     
            return rectangle;
        }
     
        public Point GetDragVisualOffset(DragVisualProviderState state)
        {
            return new Point();
        }
     
        public bool UseDefaultCursors
        {
            get { return true; }
        }
    }
  3. DevCraft banner
Back to Top