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 cbordeman@gmail.comif(template==null){template=newDataTemplate();varfactory=newFrameworkElementFactory(typeof(Image));varbs=CaptureScreen(m_DragInfo.VisualSourceItem);factory.SetValue(Image.SourceProperty,bs);if(m_DragInfo.VisualSourceItemisFrameworkElement){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 cbordeman@gmail.comUIElementrootElement=null;WindowparentWindow=m_DragInfo.VisualSource.GetVisualAncestor<Window>();UIElementadornment=null;if(parentWindow!=null){rootElement=parentWindow.ContentasUIElement;}if(rootElement==null){rootElement=(UIElement)Application.Current.MainWindow.Content;}if(m_DragInfo.DataisIEnumerable&&!(m_DragInfo.Dataisstring)){if(((IEnumerable)m_DragInfo.Data).Cast<object>().Count()<=10){ItemsControlitemsControl=newItemsControl();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=newBorder();border.Child=itemsControl;adornment=border;}}else{ContentPresentercontentPresenter=newContentPresenter();contentPresenter.Content=m_DragInfo.Data;contentPresenter.ContentTemplate=template;adornment=contentPresenter;}if(adornment!=null){adornment.Opacity=0.5;DragAdorner=newDragAdorner(rootElement,adornment);}}// Helper to generate the image - I grabbed this off Google // somewhere. -- Chris Bordeman cbordeman@gmail.comprivatestaticBitmapSourceCaptureScreen(Visualtarget,doubledpiX=96.0,doubledpiY=96.0){if(target==null)returnnull;varbounds=VisualTreeHelper.GetDescendantBounds(target);varrtb=newRenderTargetBitmap((int)(bounds.Width*dpiX/96.0),(int)(bounds.Height*dpiY/96.0),dpiX,dpiY,PixelFormats.Pbgra32);vardv=newDrawingVisual();using(varctx=dv.RenderOpen()){varvb=newVisualBrush(target);ctx.DrawRectangle(vb,null,newRect(newPoint(),bounds.Size));}rtb.Render(dv);returnrtb;}
From cborde...@gmail.com 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: cborde...@gmail.com (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: