在这段代码中,我有2个椭圆,每个都包含在自己的拇指控件中。在代码隐藏中,Thumb_DragDelta允许我自由移动对象。我的问题是: -
这是xaml: -
<Window x:Class="DragDropExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DragDropExample"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Canvas>
<Thumb x:Name="Thumb1" Canvas.Left="100"
Canvas.Top="60"
DragDelta="Thumb_DragDelta"
Panel.ZIndex="1">
<Thumb.Template>
<ControlTemplate>
<Ellipse x:Name="Ellipse1"
Fill="#FF6F1D9E"
Height="85"
Canvas.Left="379"
Stroke="Black"
Canvas.Top="20"
Width="84"
HorizontalAlignment="Right"
Margin="0,40,36.5,0"
VerticalAlignment="Top" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb x:Name="Thumb2" Canvas.Left="400"
Canvas.Top="60"
DragDelta="Thumb_DragDelta"
Panel.ZIndex="0"
AllowDrop="True">
<Thumb.Template>
<ControlTemplate>
<Ellipse x:Name="Ellipse2"
Fill="#FF9E1D40"
Height="85"
Canvas.Left="379"
Stroke="Black"
Canvas.Top="20"
Width="84"
HorizontalAlignment="Right"
Margin="0,40,36.5,0"
VerticalAlignment="Top" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
</Grid>
</Window>
这是代码隐藏中的C#: -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DragDropExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
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);
}
}
}