WPF从列表框中拖放对象

WPF从列表框中拖放对象

问题描述:

初学者在这里。WPF从列表框中拖放对象

我试图创建一个用户控件与在其他控制一个列表框,我想这个列表框允许拖放到该用户控件的其他类似的实例。

这是另一个我想拖动对象,从一个列表框下拉:

[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"); 
      } 
     } 
    } 
} 

这里是我如何处理拖放:

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; 
     } 
    } 

一切除了当细发生丢弃事件,BoxItem由于某种原因始终为空,因此没有任何内容添加到列表框中。

任何提示?

谢谢

+0

你试过从https://www.nuget.org/packages/gong-wpf-dragdrop/锣WPF-的DragDrop?源代码也awailable,所以你可以看看它是如何实现的 – bamanow

+0

@bamanow,的的NuGet锣WPF取决于WPFToolkit包,这个包依赖于.NET 3.5。它与.NET 4.0或更高版本不兼容。 –

+0

@Eriawan你错了,而不是与.NET 4.0开始就没有任何依赖性 – bamanow

的数据对象的数据应该是一个ListBoxFileName,而不是一个ListBoxItem:

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.DataContext as ListBoxFileName); 
    DragDrop.DoDragDrop(_dragged, obj, DragDropEffects.All); 
} 

private void FileListBox_Drop(object sender, DragEventArgs e) 
{ 
    if (e.Data.GetDataPresent(DataFormats.Serializable, true)) 
    { 
     ListBoxFileName BoxItem = e.Data.GetData(DataFormats.Serializable) as ListBoxFileName; 
     //... 
    } 
} 

这应该假设“FileNameList”控制的ItemsSource设置为一个IEnumerable。

请提供能够从头开始重现您的问题,如果您需要在此进一步帮助需要所有相关的代码片段。

+0

由于它是“部分”的工作。事实上,现在当我选择BoxItem的一个副本时,它在每个列表框中被选中。任何想法可能来自哪里? – lecloneur

+0

相同的ListBoxFileName对象是两个不同的可视容器的DataContext,因此当您设置其IsSelected属性(或任何您称之为)时,两个容器将被选中,因为它们绑定到相同的属性。如果你不想要这个,你应该在将它添加到第二个ListBox之前克隆,即创建一个ListBoxFileName对象的副本。 – mm8

+0

它的工作,谢谢 – lecloneur