我拖动的对象没有出现在 canvas 上?
My dragged objects don't appear on the canvas?
当我 运行 程序并尝试将椭圆拖放到 canvas 上时,椭圆没有出现在 canvas 上。虽然鼠标光标显示我正在拖动某些东西并且可以放在 canvas 上。
有什么想法吗?
已解决:原来我是想放在它来自的对象上而不是canvas我想把它放在(Ellipse
而不是 Canvas
)!
感谢您的解决方案和信息 link 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"/>
原来我是想把它放在它来自的物体上而不是 canvas 我希望它被放在上面!
感谢您的解决方案和信息link Herdo。
当我 运行 程序并尝试将椭圆拖放到 canvas 上时,椭圆没有出现在 canvas 上。虽然鼠标光标显示我正在拖动某些东西并且可以放在 canvas 上。 有什么想法吗?
已解决:原来我是想放在它来自的对象上而不是canvas我想把它放在(Ellipse
而不是 Canvas
)!
感谢您的解决方案和信息 link 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"/>
原来我是想把它放在它来自的物体上而不是 canvas 我希望它被放在上面!
感谢您的解决方案和信息link Herdo。