Click here to Skip to main content
13,737,037 members
Rate this:
 
Please Sign up or sign in to vote.
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:

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 9-Nov-17 23:31pm
Updated 10-Nov-17 19: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.
Jochen Arndt 10-Nov-17 7:53am
   
Then I guess the problem is within your application (wrong effect returned).

But your logging shows that you have set the none effect. So it can be altered only later.

What happens if you always set None in the handler?
If you then still get the can drop cursor it proves that it is set somewhere else.
Patrick Skelton 10-Nov-17 8:00am
   
You are right, Jochen. If I take out the workaround I mentioned above and I make both parts of if{}else{} return the None effect, I still get the can-drop over the ListBox. So my event handler is being ignored. Something else is simply using the AllowDrop="True" property on the ListBox to set the can-drop symbol.
Jochen Arndt 10-Nov-17 8:21am
   
Your handler is called but the effect setting is ignored or changed somewhere else later.

I have no more ideas (not enough knowledge on how drag & drop is handled by XAML applications / list boxes).

Hopefully somebody else can help.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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>


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;
	}
}
  Permalink  
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).
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180920.1 | Last Updated 11 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100