【发布时间】:2011-04-15 20:45:40
【问题描述】:
我有一个画布,可以在上面放置一些工具来创建图表(位于 tabitem2 中)。
我想要做的是,当一个工具被放在画布上时,会将一个吐出文本的事件关联到一个文本框(位于 tabitem3)。
XAML:
<ListBox>
<ListBox.Resources>
<Style TargetType="{x:Type Image}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
<EventSetter Event="MouseLeftButtonDown" Handler="DragImage"/>
</Style>
</ListBox.Resources>
<ListBoxItem>
<Image Source="toolitem1.png"></Image>
</ListBoxItem>
</ListBox>
<Canvas x:Name="Canvas" AllowDrop="True" Background="Aqua" Drop="DropImage"/>
代码背后:
private void DragImage(object sender, MouseButtonEventArgs e)
{
Image image = e.Source as Image;
DataObject data = new DataObject(typeof(ImageSource), image.Source);
DragDrop.DoDragDrop(image, data, DragDropEffects.Copy);
}
private void DropImage(object sender, DragEventArgs e)
{
ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;
Image imageControl = new Image() { Width = image.Width, Height = image.Height, Source = image };
Canvas.SetLeft(imageControl, e.GetPosition(this.Canvas).X);
Canvas.SetTop(imageControl, e.GetPosition(this.Canvas).Y);
this.Canvas.Children.Add(imageControl);
}
更新:
添加了一些示例代码并尝试了以下人员的建议,但无济于事。看起来当我尝试在我的 ListBoxItem 对象中使用 DragDrop.Drop 时,它会覆盖我的画布的 DropImage 事件,所以我仍然卡住了。
【问题讨论】:
标签: c# wpf drag-and-drop