WPF ListBoxView Problem
-
I' making a very simple app to drag files from my desktop or explorer into a WPF ListBoxView, these files will be auto backed up on a specific day and time. I have the dragging files into the ListBoxView working fine. Can even insert Multiple selected at one time. I can rearrange my list order by dragging and dropping fine as well. My issue is I can't seem to trigger it to remove the file I drag out of the ListBoxView control to remove it from the List. Can anyone help ?? My Xaml
My C#
private void DropList_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string data in files)
{
if (!dropList.Items.Contains(data))
dropList.Items.Add(data);
}
}
else
{
object data = e.Data.GetData(typeof(string));
dragSource.Items.Remove(data);
dropList.Items.Add(data);
}
}private void DropList\_MouseMove(object sender, MouseEventArgs e) { // Get the current mouse position Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemP
-
I' making a very simple app to drag files from my desktop or explorer into a WPF ListBoxView, these files will be auto backed up on a specific day and time. I have the dragging files into the ListBoxView working fine. Can even insert Multiple selected at one time. I can rearrange my list order by dragging and dropping fine as well. My issue is I can't seem to trigger it to remove the file I drag out of the ListBoxView control to remove it from the List. Can anyone help ?? My Xaml
My C#
private void DropList_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string data in files)
{
if (!dropList.Items.Contains(data))
dropList.Items.Add(data);
}
}
else
{
object data = e.Data.GetData(typeof(string));
dragSource.Items.Remove(data);
dropList.Items.Add(data);
}
}private void DropList\_MouseMove(object sender, MouseEventArgs e) { // Get the current mouse position Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemP
Quote:
My issue is I can't seem to trigger it to remove the file I drag out of the ListBoxView control to remove it from the List.
If you are just "removing", there is no "dragging", unless it's to a "trash bin". Then do the "remove" in the drop event for the "trash". Otherwise, you simply delete the entry; usually with a right-click context menu or a delete key. (Though a trash bin does not make sense in this context because you are dealing with "file names", and not actual files in terms of what's removed).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
Quote:
My issue is I can't seem to trigger it to remove the file I drag out of the ListBoxView control to remove it from the List.
If you are just "removing", there is no "dragging", unless it's to a "trash bin". Then do the "remove" in the drop event for the "trash". Otherwise, you simply delete the entry; usually with a right-click context menu or a delete key. (Though a trash bin does not make sense in this context because you are dealing with "file names", and not actual files in terms of what's removed).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
Thank you I managed to get it to work using the Drag to trash like you suggested. I simply added a Stack panel that stretched Horizontally to give the user plenty of room to click on added an icon and label, works perfect. Although if you help me figure out how to catch multiple selected files being dragged from the ListBoxView to the trash It would be better, right now it's single file only. Having the option to drag one or multi is nice. I will go ahead and mark this solved since you did help me solve the original issue.
-
Thank you I managed to get it to work using the Drag to trash like you suggested. I simply added a Stack panel that stretched Horizontally to give the user plenty of room to click on added an icon and label, works perfect. Although if you help me figure out how to catch multiple selected files being dragged from the ListBoxView to the trash It would be better, right now it's single file only. Having the option to drag one or multi is nice. I will go ahead and mark this solved since you did help me solve the original issue.
For a multi-select, you should start thinking of using an ObservableCollection and updating that; that way changes are reflected back to the ListBox and you're not updating the ListBox yourself (and can ignore any ListBox events). The multi-selections represent keys into your ObservableCollection; which could br shadowed with a dictionary.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal