为什么我在列表框中出现错误的拖动鼠标图标? [英] Why am I getting the wrong drag-over mouse icon in a listbox?

查看:87
本文介绍了为什么我在列表框中出现错误的拖动鼠标图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListBox ,它显示真实物理目录中的文件。有时候,没有选择任何目录,在这种情况下, ListBox 仍为空白。



我的代码是允许用户将文件复制到正在显示的目录中。当我这样做时,我会显示正确的鼠标图标,向用户表明这是一个有效的动作。



当没有显示有效目录时,用户仍然可以尝试将文件放入 ListBox 。代码正确地忽略了这样的请求,但我似乎无法让它显示正确的鼠标图标以向用户显示他们正在尝试做的事情没有意义。



这是应该控制鼠标图标的代码:



XAML

< ListBox x:Name =ListBoxFilesItemsSource ={Binding Files}AllowDrop =TrueSelectionMode =Single
DragOver =ListBox_Files_DragOver
Drop =ListBox_Files_Drop
MouseLeftButtonUp =ListBoxFiles_MouseLeftButtonUp
MouseDoubleClick =ListBox_Files_MouseDoubleClick>
< / ListBox>





在代码隐藏中:



  private   void  ListBox_Files_DragOver( object  sender,DragEventArgs e)
{
if (GetWindowLogic< MainWindowLogic>()。ValidProjectsDirectory&& ; e.Data.GetDataPresent(DataFormats.FileDrop))
{
QA.LogTrace($ 获得了有效的目录和数据。);
e.Effects = DragDropEffects.Copy;
}
else
{
QA.LogTrace($ 缺少有效的目录或数据。);
e.Effects = DragDropEffects.None;
}
}





请注意,我在日志文件中收到了预期的跟踪消息,但 DragDropEffects.None 似乎不起作用。



谁能告诉我我做错了什么?



我尝试过:



上面显示的代码。

解决方案

获得了有效的目录和数据。);
e.Effects = DragDropEffects.Copy;
}
else
{
QA.LogTrace(


缺少有效的目录或数据。);
e.Effects = DragDropEffects.None;
}
}





请注意,我在日志文件中收到了预期的跟踪消息,但 DragDropEffects.None 似乎不起作用。



谁能告诉我我做错了什么?



我尝试过:



上面显示的代码。


似乎解决方案是:处理拖动事件



我的代码现在是......



< 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(

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.

解决方案

"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.


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(


这篇关于为什么我在列表框中出现错误的拖动鼠标图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆