You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I made a quick modification to the DragDrop.CreateDragAdorner() method so that by default (when an adorner template is not specified), an image is extracted from the dragged item(s) and that is used as the adorner instead.
I'm not sure why this was never added as a feature before, it's so easy to implement...
Here is my modification to that method, plus a new helper method to do the image generation:
staticvoidCreateDragAdorner(){vartemplate= GetDragAdornerTemplate(m_DragInfo.VisualSource);// This block was added to create a Data Template on the fly with an image generated// from m_DragInfo.VisualSourceItem. -- Chris Bordeman [email protected]if(template==null){template=new DataTemplate();varfactory=new FrameworkElementFactory(typeof(Image));varbs= CaptureScreen(m_DragInfo.VisualSourceItem);
factory.SetValue(Image.SourceProperty, bs);if(m_DragInfo.VisualSourceItem is FrameworkElement){
factory.SetValue(FrameworkElement.WidthProperty,((FrameworkElement) m_DragInfo.VisualSourceItem).ActualWidth);
factory.SetValue(FrameworkElement.HeightProperty,((FrameworkElement)m_DragInfo.VisualSourceItem).ActualHeight);}
template.VisualTree =factory;}// The rest of the code was originally inside an "if (template != null)" // block. -- Chris Bordeman [email protected]UIElementrootElement=null;WindowparentWindow= m_DragInfo.VisualSource.GetVisualAncestor<Window>();UIElementadornment=null;if(parentWindow!=null){rootElement= parentWindow.Content as UIElement;}if(rootElement==null){rootElement=(UIElement)Application.Current.MainWindow.Content;}if(m_DragInfo.Data isIEnumerable&&!(m_DragInfo.Data isstring)){if(((IEnumerable)m_DragInfo.Data).Cast<object>().Count() <= 10){ItemsControlitemsControl=new ItemsControl();
itemsControl.ItemsSource =(IEnumerable)m_DragInfo.Data;
itemsControl.ItemTemplate =template;// The ItemsControl doesn't display unless we create a border to contain it.// Not quite sure why this is...Borderborder=new Border();
border.Child =itemsControl;adornment=border;}}else{ContentPresentercontentPresenter=new ContentPresenter();
contentPresenter.Content = m_DragInfo.Data;
contentPresenter.ContentTemplate =template;adornment=contentPresenter;}if(adornment!=null){
adornment.Opacity =0.5;DragAdorner=new DragAdorner(rootElement, adornment);}}// Helper to generate the image - I grabbed this off Google // somewhere. -- Chris Bordeman [email protected]privatestatic BitmapSource CaptureScreen(Visualtarget,doubledpiX=96.0,doubledpiY=96.0){if(target==null)returnnull;varbounds= VisualTreeHelper.GetDescendantBounds(target);varrtb=new RenderTargetBitmap((int)(bounds.Width *dpiX/96.0),(int)(bounds.Height *dpiY/96.0),
dpiX,
dpiY,
PixelFormats.Pbgra32);vardv=new DrawingVisual();using(varctx= dv.RenderOpen()){varvb=new VisualBrush(target);
ctx.DrawRectangle(vb,null,new Rect(new Point(), bounds.Size));}
rtb.Render(dv);returnrtb;}
From [email protected] on June 28, 2011 17:44:10
One mistake: should also set the alignments of the generated image control in addition to setting the ActualWidth/Height:
Original author: [email protected] (June 28, 2011 17:10:07)
I made a quick modification to the DragDrop.CreateDragAdorner() method so that by default (when an adorner template is not specified), an image is extracted from the dragged item(s) and that is used as the adorner instead.
I'm not sure why this was never added as a feature before, it's so easy to implement...
Here is my modification to that method, plus a new helper method to do the image generation:
Original issue: http://code.google.com/p/gong-wpf-dragdrop/issues/detail?id=38
The text was updated successfully, but these errors were encountered: