Anfänger hier.WPF Drag & Drop-Objekt aus Listbox
Ich versuche, eine Benutzersteuerung mit einer Listbox unter anderen Steuerelement zu erstellen, und ich möchte diese Listbox Drag-and-Drop zu anderen ähnlichen Instanz der Benutzersteuerung zulassen.
Dies ist das Objekt, das ich von einem listbox zum anderen ziehen und ablegen wollen:
[Serializable]
public class ListBoxFileName : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private string FileNameValue;
public string FileName
{
get { return this.FileNameValue; }
set
{
if (value != this.FileNameValue)
{
this.FileNameValue = value;
NotifyPropertyChanged("FileName");
}
}
}
private bool FileIsSelectedValue;
public bool FileIsSelected
{
get { return this.FileIsSelectedValue; }
set
{
if (value != this.FileIsSelectedValue)
{
this.FileIsSelectedValue = value;
NotifyPropertyChanged("FileIsSelected");
}
}
}
}
Hier ist, wie ich mit der Drag & Drop befassen:
private ListBoxItem _dragged;
private void FileNameList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_dragged != null)
return;
UIElement element = FileNameList.InputHitTest(e.GetPosition(FileNameList)) as UIElement;
while (element != null)
{
if (element is ListBoxItem)
{
_dragged = (ListBoxItem)element;
break;
}
element = VisualTreeHelper.GetParent(element) as UIElement;
}
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (_dragged == null)
return;
if (e.LeftButton == MouseButtonState.Released)
{
_dragged = null;
return;
}
DataObject obj = new DataObject(DataFormats.Serializable, _dragged);
DragDrop.DoDragDrop(_dragged, obj, DragDropEffects.All);
}
private void FileNameList_DragEnter(object sender, DragEventArgs e)
{
if (_dragged == null || e.Data.GetDataPresent(DataFormats.Serializable, true) == false)
e.Effects = DragDropEffects.None;
else
e.Effects = DragDropEffects.All;
}
private void FileListBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
{
string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
for (var i = 0; i < droppedFilePaths.Length; i++)
{
ListBoxFileName filename = new ListBoxFileName();
filename.FileName = droppedFilePaths[i];
filename.FileIsSelected = false;
FileNamesItems.Add(filename);
}
}
if (e.Data.GetDataPresent(DataFormats.Serializable, true))
{
ListBoxFileName BoxItem = new ListBoxFileName();
BoxItem = e.Data.GetData(DataFormats.Serializable) as ListBoxFileName;
}
}
Alles ist in Ordnung, außer wenn Das Drop-Ereignis tritt auf, BoxItem
ist aus irgendeinem Grund immer Null, daher wird der Listbox nichts hinzugefügt.
Irgendwelche Hinweise?
Danke
Haben Sie gong-wpf-dragdrop aus https://www.nuget.org/packages/gong-wpf-dragdrop/ probiert? Quellcode ist auch verfügbar, so dass Sie sehen können, wie es implementiert wurde – bamanow
@bamanow, das nugget von gong-wpf hängt vom WPFToolkit-Paket ab, und dieses Paket hängt von .NET 3.5 ab. Es ist nicht kompatibel mit .NET 4.0 oder höher. –
@Eriawan du irrst dich, stattdessen mit .NET 4.0 hat es keine Abhängigkeiten – bamanow