1
我正在嘗試將ListView
項目拖放到該存儲在該ListView
項目中的文件副本中。我在開始拖動時成功從ListView
項目獲取位置,但無法向操作系統發出信號以將該文件複製到指定位置。無法將文件從WPF ListView拖動到Windows資源管理器
private Point start;
ListView dragSource = null;
private void files_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.start = e.GetPosition(null);
ListView parent = (ListView)sender;
dragSource = parent;
object data = GetDataFromListBox(dragSource, e.GetPosition(parent));
Hide();
if (data != null)
{
string dataStr = ((UserData)data).Data.ToString();
string filepath = new System.IO.FileInfo(dataStr).FullName;
DataObject fileDrop = new DataObject(DataFormats.FileDrop, filepath);
DragDrop.DoDragDrop((ListView)sender, fileDrop, DragDropEffects.Copy);
}
}
private static object GetDataFromListBox(ListView source, Point point)
{
UIElement element = source.InputHitTest(point) as UIElement;
if (element != null)
{
object data = DependencyProperty.UnsetValue;
while (data == DependencyProperty.UnsetValue)
{
data = source.ItemContainerGenerator.ItemFromContainer(element);
if (data == DependencyProperty.UnsetValue)
{
element = VisualTreeHelper.GetParent(element) as UIElement;
}
if (element == source)
{
return null;
}
}
if (data != DependencyProperty.UnsetValue)
{
return data;
}
}
return null;
}
第二種方法GetDataFromListBox()
我在做題的回答的一個發現。此方法從ListBox
或ListView
中提取正確的數據。
我是WPF的新手。請告訴我我錯過了什麼?