0
當我運行程序並嘗試在畫布上拖放一個橢圓時,橢圓不會出現在畫布上。儘管鼠標光標顯示我正在拖動某些東西,但可以放在畫布上。 任何想法?我的拖動對象不會出現在畫布上?
解決:原來我試圖把它從不是我想要的畫布它要在來到丟棄在對象上滴(的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"/>
你必須訂閱'AchtergrondCanvas'的事件'Drop',而不是'Ellipse'。有關拖放的非常好的指南可以在[WPF教程](http://www.wpftutorial.net/draganddrop.html)上找到。 – Herdo