Click here to Skip to main content
15,509,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListBox that displays the files in a real physical directory. Sometimes, no directory has been selected, and in this case the ListBox remains blank.

I have code that allows users to copy files into the directory being displayed. When I do this, I get the correct mouse icon displayed, to indicate to the user that this is a valid action.

When no valid directory is being displayed, the user can still attempt to drop files into the ListBox. The code correctly ignores such a request, but I don't seem to be able to get it to display the correct mouse icon to show the user that what they are trying to do doesn't make sense.

This is the code that is supposed to control the mouse icon:

XAML
<ListBox x:Name="ListBoxFiles" ItemsSource="{Binding Files}" AllowDrop="True" SelectionMode="Single"
	DragOver="ListBox_Files_DragOver"
	Drop="ListBox_Files_Drop"
	MouseLeftButtonUp="ListBoxFiles_MouseLeftButtonUp"
	MouseDoubleClick="ListBox_Files_MouseDoubleClick">
</ListBox>


In the code-behind:

C#
private void ListBox_Files_DragOver( object sender, DragEventArgs e )
{
	if( GetWindowLogic<MainWindowLogic>().ValidProjectsDirectory && e.Data.GetDataPresent( DataFormats.FileDrop ) )
	{
		QA.LogTrace( $"Got a valid directory and data." );
		e.Effects = DragDropEffects.Copy;
	}
	else
	{
		QA.LogTrace( $"Either a valid directory or data is absent." );
		e.Effects = DragDropEffects.None;
	}
}


Note that I get the expected trace messages in my log file, but DragDropEffects.None doesn't seem to work.

Can anyone tell me what I am doing wrong?

What I have tried:

The code shown in the text above.
Posted
Updated 10-Nov-17 20:15pm
Comments
Jochen Arndt 10-Nov-17 6:23am    
I don't know if I understand the problem.
When dragging around while dropping is not possible, that is indicated by the cursor (or the stop sign). Once you enter an area where dropping is possible, the cursor changes.

But if your list is empty, the cursor should already indicate that dropping is not possible before entering the list and should therefore not change when over the list.

From your description I would think that the cursor always changes to "can drop" when entering your list even when it is empty. If that is the case it is a weird behaviour because your code looks fine.
Patrick Skelton 10-Nov-17 7:31am    
Yes, Jochen, that is exactly what happens. I get the No-Drop symbol when I drag over any area of my desktop that won't accept the drop. When I move over my main application window, I still get the No-Drop. But as soon as I go over the ListBox, I get a Can-Drop symbol.

I am guessing that my else{} is doing what it is supposed to but maybe something higher or lower in the XAML visual tree is overriding the behaviour.

I'll keep poking around, to see if I can find anything.
Jochen Arndt 10-Nov-17 7:43am    
Thank you for the clarification.

I don't know XAML, so I might be not of much help.

Tow more questions:
What is the drag source (Windows Explorer or your / another app)?
Changes the cursor back to no drop when leaving your (empty) list?

Some background information:
The function that is responsible for drawing the cursor is the drag & drop feedback implementation of the drag source that uses the effect passed from the drop target handlers (enter, over, drop, and leave).
Patrick Skelton 10-Nov-17 7:47am    
The drag-source is Explorer. The cursor does change back to Stop-symbol when leaving the ListBox. Thank you for the extra information.
Patrick Skelton 10-Nov-17 7:37am    
A workaround is to bind the IsEnabled property of the ListBox to the boolean property ValidProjectsDirectory, which is set true only when the file pane is showing the contents of a directory.

It seems the solution is this: Handle Drag Events

My code now is...

<ListBox x:Name="ListBoxFiles"
	ItemsSource="{Binding Files}"
	AllowDrop="True"
	SelectionMode="Single"
	DragEnter="ListBox_Files_DragEnterOverLeave"
	DragOver="ListBox_Files_DragEnterOverLeave"
	DragLeave="ListBox_Files_DragEnterOverLeave"
	Drop="ListBox_Files_Drop"
	MouseLeftButtonUp="ListBoxFiles_MouseLeftButtonUp"
	MouseDoubleClick="ListBox_Files_MouseDoubleClick">
</ListBox>


C#
private void ListBox_Files_DragEnterOverLeave( object sender, DragEventArgs e )
{
	if( GetWindowLogic<MainWindowLogic>().ValidProjectsDirectory && e.Data.GetDataPresent( DataFormats.FileDrop ) )
	{
		QA.LogTrace( $"Got a valid directory and data in event handler." );
		e.Effects = DragDropEffects.Copy;
	}
	else
	{
		QA.LogTrace( $"Either a valid directory or data is absent in event handler." );
		e.Effects = DragDropEffects.None;
		e.Handled = true;
	}
}
 
Share this answer
 
Comments
Jochen Arndt 10-Nov-17 8:52am    
Fine to see you you got it solved (should have had the idea that all handlers must be implemented).
<ListBox x:Name="ListBoxFiles"
	ItemsSource="{Binding Files}"
	AllowDrop="True"
	SelectionMode="Single"
	DragEnter="ListBox_Files_DragEnterOverLeave"
	DragOver="ListBox_Files_DragEnterOverLeave"
	DragLeave="ListBox_Files_DragEnterOverLeave"
	Drop="ListBox_Files_Drop"
	MouseLeftButtonUp="ListBoxFiles_MouseLeftButtonUp"
	MouseDoubleClick="ListBox_Files_MouseDoubleClick">
</ListBox>






private void ListBox_Files_DragEnterOverLeave( object sender, DragEventArgs e )
{
if( GetWindowLogic<mainwindowlogic>().ValidProjectsDirectory && e.Data.GetDataPresent( DataFormats.FileDrop ) )
{
QA.LogTrace( $"Got a valid directory and data in event handler." );
e.Effects = DragDropEffects.Copy;
}
else
{
QA.LogTrace( $"Either a valid directory or data is absent in event handler." );
e.Effects = DragDropEffects.None;
e.Handled = true;
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900