Also, das Problem konnte ich im wahrsten Sinne des Wortes teilweise nachvollziehen, es trat bei mir bei jedem zweiten Klick auf. Also bei jedem zweiten Klick wird das Event gefeuert. (Allerdings ist das XAML bei mir nicht kompilierbar, in der Canvas muss es
x:Name heißen).
Mir ist dann aufgefallen, dass Deinen Move-Methode nicht wirklich optimal ist, weil sie einen Haufen Rectangles erzeugt und wegwirft, obwohl Du nur eins brauchst. Wenn Du die Move-Methode durch unten stehende ersetzt, funktioniert merkwürdigerweise auch das MouseDown. Ich habe aber nicht wirklich einen Plan, warum.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
| private void Canvas1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { IInputElement displayArea = null;
Point pt = e.GetPosition(displayArea);
if (e.LeftButton != MouseButtonState.Pressed) { Rect re1 = new Rect(pt.X, pt.Y, 30, 30);
var myRect = Canvas1.Children.OfType<Rectangle>().FirstOrDefault(r => r.Name == "Rectangle"); if (myRect == null) { myRect = new Rectangle();
myRect.Fill = Brushes.AliceBlue; myRect.Stroke = Brushes.Bisque;
myRect.StrokeThickness = 1;
myRect.Width = re1.Right - re1.Left; myRect.Height = re1.Bottom - re1.Top;
myRect.Name = "Rectangle";
Canvas1.Children.Add(myRect);
}
Canvas.SetLeft(myRect, re1.Left); Canvas.SetTop(myRect, re1.Top);
MainForm.Title = pt.X.ToString() + ", " + pt.Y.ToString(); } } |
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".