我有一个带有这个 xaml 的 WPF 窗口:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Border" x:Key="BorderStyle">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
<Style TargetType="Image" x:Key="ImageStyle">
<Setter Property="Height" Value="75"/>
<Setter Property="Width" Value="75"/>
<Setter Property="AllowDrop" Value="True"/>
</Style>
</Window.Resources>
<Grid>
<GroupBox>
<Grid>
<ListBox x:Name="listbox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding}"/>
<Border Style="{StaticResource BorderStyle}">
<Image Style="{StaticResource ImageStyle}" Drop="ThisDrop"/>
</Border>
<Border Style="{StaticResource BorderStyle}">
<Image Style="{StaticResource ImageStyle}" Drop="ThatDrop"/>
</Border>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</GroupBox>
</Grid>
</Window>
而这背后的代码:
using System.Linq;
namespace WpfApplication2
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
listbox.ItemsSource = Enumerable.Range(1, 3);
}
private void ThisDrop(object sender, System.Windows.DragEventArgs e)
{
// do something
}
private void ThatDrop(object sender, System.Windows.DragEventArgs e)
{
// do something else
}
}
}
当我从 Windows 资源管理器将文件拖放到图像控件上时,我很难让这些放置事件触发。