This is a migrated thread and some comments may be shown as answers.

DragVisual: White Background

1 Answer 73 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 28 Nov 2012, 05:33 AM
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

1 Answer, 1 is accepted

Sort by
0
Steven
Top achievements
Rank 1
answered on 29 Nov 2012, 04:20 PM
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; }
    }
}
Tags
DragAndDrop
Asked by
Steven
Top achievements
Rank 1
Answers by
Steven
Top achievements
Rank 1
Share this question
or