z112
(Wei)
October 31, 2023, 2:15am
1
Hi
I use a wpf sample to create my own plugin for rhino, the function is a wpf contains a treeview, this treeview load some gh files in a folder. I would like to drag the gh file into active grasshopper file, but when i drag, it seems doesn’t work. Belows are dragleave event, I think the code should be ok, the reason is probably related to my sample, the sample i used is SampleCSwpf which may have some special setting. i use the I am a new developer. Can anybody help? Thank you very much!
private void treeView1_DragLeave(object sender, DragEventArgs e)
{
string file = "C:\\Users\\DIY\\Desktop\\Project\\Buildings" + treeView1.SelectedValue.ToString();
DataObject dataObject = new DataObject(DataFormats.FileDrop, new string[] { file });
DragDropEffects finalDropEffect = DragDrop.DoDragDrop(treeView1, dataObject, DragDropEffects.Move);
}
Hi @z112 ,
You should be using using the MouseLeftButtonDown or PreviewMouseLeftButtonDown event to start the drag-and-drop operation.
private void treeView1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TreeViewItem treeViewItem = FindAncestor<TreeViewItem>((DependencyObject)e.OriginalSource);
if (treeViewItem != null)
{
string file = "C:\\whatever" + treeView1.SelectedValue.ToString();
DataObject dataObject = new DataObject(DataFormats.FileDrop, new string[] { file });
DragDrop.DoDragDrop(treeView1, dataObject, DragDropEffects.Move);
}
}
// Helper to search up the VisualTree
private static T FindAncestor<T>(DependencyObject current)
where T : DependencyObject
{
do
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
}
while (current != null);
return null;
}
The target application must also be configured to accept the dragged items.
I hope that sparks some inspiration.
Farouk
z112
(Wei)
November 2, 2023, 1:13am
3
Hi Farouk,
Thank you for your reply. I tried Winform and it works. So i changed my plugin to Winform. I’ll try your answer later and back to you then.
Wei