Hi so i've got a WPF window, 3 different DataGrids and one treeview. My goal is to drag/drop an item from the treeview into a specific datagrid. I've got working code for the drag/drop and its working flawless but my problem is that i don't know how to link the event to other dataviews. So in the 'Drop' event i change the content of my first dataview but i'd like to change the content of the dataview i actually dropped the item on.
this is my code:
For every DataGrid in xaml:
Drop="Droptree_Drop" DragEnter="Droptree_DragEnter" AllowDrop="True"
My Treeview PreviewMouseMove event:
private void TvDates_PreviewMouseMove(object sender, MouseEventArgs e)
{
try
{
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
//
TreeView tree = sender as TreeView;
TreeViewItem tvitem = FindAnchestor<TreeViewItem>((DependencyObject)e.OriginalSource);
// Initialize the drag & drop operation
DataObject dragData = new DataObject("myFormat", tvDates.SelectedItem.ToString());
DragDrop.DoDragDrop(tvitem, dragData, DragDropEffects.Move);
}
}
catch (Exception)
{
}
}
Droptree_DragEnter event:
private void Droptree_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("myFormat") ||
sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
and finally my Drop event:
private void Droptree_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("myFormat"))
{
// MessageBox.Show(e.Data.GetData("myFormat") as string); Get Item content
List<string> currentitems = new List<string>();
foreach (dynamic item in dgChauffeur1.Items)
{
currentitems.Add(item.Orders.ToString());
}
currentitems.Add(e.Data.GetData("myFormat") as string);
dgChauffeur1.ItemsSource = currentitems.Select(s => new { Orders = s }).ToList(); //This is where i have my DataGrid however i want to have the datagrid i dropped my item on.
}
}
adding this code into the event fixed it:
DataGrid dg = sender as DataGrid;
dg.ItemsSource = ....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.