public void KlassGrid_MouseDown(object sender, MouseButtonEventArgs e) { MainWindow mw = (MainWindow)Application.Current.MainWindow; Keyboard.Focus(this); if (e.ClickCount == 2) { Grid g = new Grid() { Width = mw.theCanvas.ActualWidth, Height = mw.theCanvas.ActualHeight, Background = Brushes.Black, Opacity = 0.2 }; Canvas.SetLeft(g, 0); Canvas.SetTop(g, 0); ClassSettings cs = new ClassSettings(KlassViewModel, g); g.MouseDown += (sendr, eventArgs) => { cs.Save(); }; //Skapar ett mouseDown-event för Grid g som anropar CloseSettings double x = (KlassViewModel.KlassModel.PositionLeft + _posOnUserControlOnHit.X - cs.Width / 2.33); double y = (KlassViewModel.KlassModel.PositionTop + _posOnUserControlOnHit.Y - cs.Height / 2); if (cs.Width + x > mw.ActualWidth) { Canvas.SetLeft(cs, x - (x + cs.Width - mw.ActualWidth)); } else if (x < 0) { Canvas.SetLeft(cs, x - x); } else { Canvas.SetLeft(cs, x); } if (cs.Height + y > mw.ActualHeight) { Canvas.SetTop(cs, y - (y + cs.Height - mw.ActualHeight)); } else if (y < 0) { Canvas.SetTop(cs, y - y); } else { Canvas.SetTop(cs, y); } mw.theCanvas.Children.Add(g); mw.theCanvas.Children.Add(cs); } else if (!(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) { mw.DeselectAll(); } CaptureMouse(); _posOnUserControlOnHit = Mouse.GetPosition(this); KlassViewModel.Select(); e.Handled = true; }