c# - Drag and drop into grid not dropping properly -


i have found code here:

https://social.msdn.microsoft.com/forums/en-us/b01a66ef-ae83-4891-9fe9-74ed2f4fd99c/drag-and-drop-images?forum=wpf

which seems need drag , drop grid column definition.

so have that:

<window x:class="wpfapplication1.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:wpfapplication1"     mc:ignorable="d"     title="mainwindow" height="350" width="525">     <grid previewmouseleftbuttondown="grid_previewmouseleftbuttondown" background="transparent"       previewmousemove="grid_previewmousemove"       dragenter="grid_dragenter"       drop="grid_drop"       allowdrop="true">       <grid.columndefinitions>         <columndefinition/>         <columndefinition/>       </grid.columndefinitions>       <grid.rowdefinitions>         <rowdefinition/>         <rowdefinition/>       </grid.rowdefinitions>       <image x:name="ld" margin="5" grid.row="0" grid.column="0" source="greenball.ico"/>       <image x:name="iar" margin="5" grid.row="1" grid.column="0" source="greenball.ico"/>       <image x:name="ry" margin="5" grid.row="0" grid.column="1" source="greenball.ico"/>       <image x:name="dia" margin="5" grid.row="1" grid.column="1" source="greenball.ico"/>    </grid> 

, effect 1 in pic

enter image description here

the code behind is:

public partial class mainwindow : window {    private point startdragpoint;   image dragimage = new image();    public mainwindow()   {     initializecomponent();   }    private void grid_previewmouseleftbuttondown(object sender, mousebuttoneventargs e)   {     dragimage = e.source image;     startdragpoint = e.getposition(null);   }    private void grid_previewmousemove(object sender, mouseeventargs e)   {     if (e.leftbutton == mousebuttonstate.pressed)     {       point position = e.getposition(null);        if (math.abs(position.x - startdragpoint.x) >              systemparameters.minimumhorizontaldragdistance ||           math.abs(position.y - startdragpoint.y) >              systemparameters.minimumverticaldragdistance)        {         dataobject data = new dataobject(typeof(imagesource), dragimage.source);         dragdrop.dodragdrop(dragimage, data, dragdropeffects.move);       }     }   }    private void grid_dragenter(object sender, drageventargs e)   {     if (e.data.getdatapresent(typeof(dataobject)))     {       e.effects = dragdropeffects.copy;     }     else     {       e.effects = dragdropeffects.none;     } }    private void grid_drop(object sender, drageventargs e)   {     image dropimage = e.source image;     double x = dropimage.actualwidth;      dragimage.source = dropimage.source;     dropimage.source = e.data.getdata(typeof(imagesource)) imagesource;    }  } 

now when drag , drop red ball effect nothing appears

enter image description here

i no wpf expert see dropimage.source null

enter image description here

it's not necessary keep same code need put drag , drop images grid column , row definitions in advance


Comments