WPF - DragOver 事件没有在我的列表框上触发
WPF - DragOver event is not firing on my Listbox
我正在尝试在我的 WPF 应用程序中实现拖放功能。我有一个列表框,我希望能够从 Windows Explorer 中拖放文件。然后将这些文件添加到列表框中。按照本教程:Drag and drop files to WPF application and asynchronously upload to ASP.NET Web API,我一次完成了这项工作。减去上传到网络 api 部分。我只想显示列表框中的文件。
问题是 DragOver 事件永远不会触发。我错过了什么?我早些时候有这个工作,但一些小的调整搞砸了。
这是我的XAML(简化了父元素):
<Window>
<Grid>
<TabControl>
<TabItem>
<Grid>
<GroupBox>
<ListBox Name="lstTarget" AllowDrop="True"
Drop="lstTarget_Drop"
DragOver="lstTarget_DragOver"
DragLeave="lstTarget_DragLeave"
BorderThickness="3"
BorderBrush="Red"
>
</ListBox>
下面是我的代码
private void lstTarget_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string filePath in files)
{
lstTarget.Items.Add(filePath);
}
}
var listbox = sender as ListBox;
listbox.Background = new SolidColorBrush(Color.FromRgb(226, 226, 226));
}
private void lstTarget_DragOver(object sender, DragEventArgs e)
{
lstTarget.BorderBrush = Brushes.Green;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Copy;
var listbox = sender as ListBox;
listbox.Background = new SolidColorBrush(Color.FromRgb(155, 155, 155)
}
else
{
e.Effects = DragDropEffects.None;
}
}
private void lstTarget_DragLeave(object sender, DragEventArgs e)
{
var listbox = sender as ListBox;
listbox.Background = new SolidColorBrush(Color.FromRgb(226, 226, 226));
}
好吧,我是个傻瓜。如果我 运行 我的应用程序作为管理员,那么我无法从资源管理器中拖入项目。他们很可能 运行 在不同的安全上下文中。
我的代码没有问题。
我不确定问题出在哪里,但我宁愿使用 DragEnter 而不是 DragOver,因为您只需要检查一次拖动的数据内容
我正在尝试在我的 WPF 应用程序中实现拖放功能。我有一个列表框,我希望能够从 Windows Explorer 中拖放文件。然后将这些文件添加到列表框中。按照本教程:Drag and drop files to WPF application and asynchronously upload to ASP.NET Web API,我一次完成了这项工作。减去上传到网络 api 部分。我只想显示列表框中的文件。
问题是 DragOver 事件永远不会触发。我错过了什么?我早些时候有这个工作,但一些小的调整搞砸了。
这是我的XAML(简化了父元素):
<Window>
<Grid>
<TabControl>
<TabItem>
<Grid>
<GroupBox>
<ListBox Name="lstTarget" AllowDrop="True"
Drop="lstTarget_Drop"
DragOver="lstTarget_DragOver"
DragLeave="lstTarget_DragLeave"
BorderThickness="3"
BorderBrush="Red"
>
</ListBox>
下面是我的代码
private void lstTarget_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string filePath in files)
{
lstTarget.Items.Add(filePath);
}
}
var listbox = sender as ListBox;
listbox.Background = new SolidColorBrush(Color.FromRgb(226, 226, 226));
}
private void lstTarget_DragOver(object sender, DragEventArgs e)
{
lstTarget.BorderBrush = Brushes.Green;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Copy;
var listbox = sender as ListBox;
listbox.Background = new SolidColorBrush(Color.FromRgb(155, 155, 155)
}
else
{
e.Effects = DragDropEffects.None;
}
}
private void lstTarget_DragLeave(object sender, DragEventArgs e)
{
var listbox = sender as ListBox;
listbox.Background = new SolidColorBrush(Color.FromRgb(226, 226, 226));
}
好吧,我是个傻瓜。如果我 运行 我的应用程序作为管理员,那么我无法从资源管理器中拖入项目。他们很可能 运行 在不同的安全上下文中。
我的代码没有问题。
我不确定问题出在哪里,但我宁愿使用 DragEnter 而不是 DragOver,因为您只需要检查一次拖动的数据内容