我从此链接https://wpf.2000things.com/2012/12/19/715-using-the-thumb-control-to-drag-objects-on-a-canvas/中获得了以下代码
City Code
C#
<Canvas>
<Thumb Canvas.Left="10" Canvas.Top="20" Canvas.ZIndex="99" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Image Width="60" Height="60" Source="Crown.jpg"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb Canvas.Left="100" Canvas.Top="60" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Image Width="80" Height="100" Source="HenryII.jpg"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb Canvas.Left="30" Canvas.Top="180" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Label Content="Westminster, 19-Dec-1154"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
使用此代码,您可以将对象拖到private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
UIElement thumb = e.Source as UIElement;
Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
}
上。
我不希望将对象拖到Canvas
中。