当我运行程序并尝试在画布上拖放椭圆时,椭圆不会出现在画布上。虽然鼠标光标显示我正在拖动某些东西,但是可以放在画布上。 有什么想法吗?
已解决:原来我试图放弃它来自的对象,而不是我希望它被放在上面的画布(Ellipse
而不是Canvas
) !
感谢您提供解决方案和信息性链接Herdo。
代码背后:
private void Ellipse_MouseMove(object sender, MouseEventArgs e)
{
Ellipse bal = (Ellipse)sender;
if (e.LeftButton == MouseButtonState.Pressed)
{
DataObject sleepbal = new DataObject("sleepbal", bal);
DragDrop.DoDragDrop(bal, sleepbal, DragDropEffects.Copy);
}
}
private void Ellipse_Drop(object sender, DragEventArgs e)
{
Ellipse ellipse = (Ellipse)sender; ;
if (ellipse != null)
{
if (e.Data.GetDataPresent("sleepbal"))
{
string dataString = (string)e.Data.GetData(DataFormats.StringFormat);
BrushConverter converter = new BrushConverter();
if (converter.IsValid(dataString))
{
Brush nieuweKleur = (Brush)converter.ConvertFromString(dataString);
ellipse.Fill = nieuweKleur;
Positie = Mouse.GetPosition(AchtergrondCanvas);
Canvas.SetLeft(sleepBal, Positie.X);
Canvas.SetTop(sleepBal, Positie.Y);
AchtergrondCanvas.Children.Add(ellipse);
}
}
}
}
XAML:
<Canvas Name="AchtergrondCanvas" Height="400" Width="500" AllowDrop="True"></Canvas>
<Ellipse Name="VoorbeeldBal" Fill="{Binding SelectedValue, ElementName=BalKleurComboBox}"
MouseMove="Ellipse_MouseMove" Drop="Ellipse_Drop"/>