/// <summary> /// Нажатие на кнопку удаления элемента /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtDelete_Click(object sender, RoutedEventArgs e) { using (var gc = new GeometryContext()) { if (_activeShape.Type == ShapeInfo.ShapeType.Circle) { var item = new Circle {Id = _activeShape.Guid}; gc.Circles.Attach(item); gc.Circles.Remove(item); gc.SaveChanges(); } if (_activeShape.Type == ShapeInfo.ShapeType.Rectangle) { var item = new Models.GeometryShapes.Rectangle {Id = _activeShape.Guid}; gc.Rectangles.Attach(item); gc.Rectangles.Remove(item); gc.SaveChanges(); } } RefreshAll(); }
/// <summary> /// Заполняет базу данных демо даннными /// </summary> public static void Fill() { using (var gc = new GeometryContext()) { // очищаем старую базу данных foreach (var id in gc.Colors.Select(e => e.Id)) { var item = new Color {Id = id}; gc.Colors.Attach(item); gc.Colors.Remove(item); } gc.SaveChanges(); foreach (var id in gc.Positions.Select(e => e.Id)) { var item = new Position {Id = id}; gc.Positions.Attach(item); gc.Positions.Remove(item); } gc.SaveChanges(); foreach (var id in gc.Circles.Select(e => e.Id)) { var item = new Circle {Id = id}; gc.Circles.Attach(item); gc.Circles.Remove(item); } gc.SaveChanges(); foreach (var id in gc.Rectangles.Select(e => e.Id)) { var item = new Rectangle {Id = id}; gc.Rectangles.Attach(item); gc.Rectangles.Remove(item); } gc.SaveChanges(); // добавляем цвета var colorList = new List<Color> { new Color {Title = "Red"}, new Color {Title = "Green"}, new Color {Title = "Blue"} }; colorList.ForEach(item => gc.Colors.Add(item)); gc.SaveChanges(); // добавляем координаты var positionList = new List<Position> { new Position {CoordX = 200, CoordY = 100}, new Position {CoordX = 390, CoordY = 170}, new Position {CoordX = 230, CoordY = 380}, new Position {CoordX = 290, CoordY = 120}, new Position {CoordX = 170, CoordY = 130}, new Position {CoordX = 170, CoordY = 160}, new Position {CoordX = 140, CoordY = 100}, new Position {CoordX = 105, CoordY = 340}, new Position {CoordX = 200, CoordY = 450} }; positionList.ForEach(item => gc.Positions.Add(item)); gc.SaveChanges(); // добавляем круги var circleList = new List<Circle> { new Circle {Radius = 40, Color = colorList[0], Position = positionList[0]}, new Circle {Radius = 50, Color = colorList[1], Position = positionList[1]}, new Circle {Radius = 60, Color = colorList[2], Position = positionList[2]}, new Circle {Radius = 70, Color = colorList[0], Position = positionList[3]}, new Circle {Radius = 80, Color = colorList[1], Position = positionList[4]} }; circleList.ForEach(item => gc.Circles.Add(item)); gc.SaveChanges(); // добавляем прямоугольники var rectangleList = new List<Rectangle> { new Rectangle {Width = 40, Height = 70, Color = colorList[0], Position = positionList[5]}, new Rectangle {Width = 20, Height = 20, Color = colorList[1], Position = positionList[6]}, new Rectangle {Width = 30, Height = 30, Color = colorList[2], Position = positionList[7]}, new Rectangle {Width = 70, Height = 50, Color = colorList[0], Position = positionList[8]} }; rectangleList.ForEach(item => gc.Rectangles.Add(item)); gc.SaveChanges(); } }
/// <summary> /// Нажатие на кнопку ОК /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonOk_Click(object sender, RoutedEventArgs e) { using (var gc = new GeometryContext()) { // блок исключающий возникновение чрезвычайных ситуаций // например конвертация неподходящей строки в число try { // вначале удаляем старую запись, если такая имела место быть if (_guid.HasValue) { if (_shapeType == ShapeInfo.ShapeType.Circle) { var item = new Circle {Id = _guid.Value}; gc.Circles.Attach(item); gc.Circles.Remove(item); } if (_shapeType == ShapeInfo.ShapeType.Rectangle) { var item = new Rectangle {Id = _guid.Value}; gc.Rectangles.Attach(item); gc.Rectangles.Remove(item); } gc.SaveChanges(); } // затем создаем новую запись о координате Position position = new Position { CoordX = Convert.ToDouble(TextBoxCoordX.Text), CoordY = Convert.ToDouble(TextBoxCoordY.Text) }; gc.Positions.Add(position); gc.SaveChanges(); // определяем какой цвет выбран string colorName; switch (ColorList.SelectedIndex) { case 0: colorName = "Red"; break; case 1: colorName = "Green"; break; default: colorName = "Blue"; break; } var color = gc.Colors.First(x => x.Title == colorName); gc.SaveChanges(); // и создаем нужную фигуру if (_shapeType == ShapeInfo.ShapeType.Circle) { Circle circle = new Circle { Position = position, Radius = Convert.ToSingle(TextBox1.Text), Color = color }; gc.Circles.Add(circle); } else { Rectangle rec = new Rectangle { Position = position, Width = Convert.ToSingle(TextBox1.Text), Height = Convert.ToSingle(TextBox2.Text), Color = color }; gc.Rectangles.Add(rec); } gc.SaveChanges(); Close(); } catch (Exception) { } } }
/// <summary> /// Инициализирует режим редактирования фигуры /// </summary> private void InitEditMode() { using (var gc = new GeometryContext()) { ShapesList.IsEnabled = false; switch (_shapeType) { // если редактируем круг case ShapeInfo.ShapeType.Circle: Label1.Content = "Radius"; Label2.Content = ""; TextBox1.IsEnabled = true; TextBox2.IsEnabled = false; // заполняем все поля информацией о редактируемой фигуре Circle circle = gc.Circles.First(x => x.Id == _guid.Value); TextBox1.Text = circle.Radius.ToString(CultureInfo.InvariantCulture); TextBoxCoordX.Text = circle.Position.CoordX.ToString(CultureInfo.InvariantCulture); TextBoxCoordY.Text = circle.Position.CoordY.ToString(CultureInfo.InvariantCulture); int colorIndex; switch (circle.Color.Title) { case "Red": colorIndex = 0; break; case "Green": colorIndex = 1; break; default: colorIndex = 2; break; } ColorList.SelectedIndex = colorIndex; break; // если редактируем прямоугольник case ShapeInfo.ShapeType.Rectangle: Label1.Content = "Width"; Label2.Content = "Height"; TextBox1.IsEnabled = true; TextBox2.IsEnabled = true; // заполняем все поля информацией о редактируемой фигуре Rectangle rectangle = gc.Rectangles.First(x => x.Id == _guid.Value); TextBox1.Text = rectangle.Width.ToString(CultureInfo.InvariantCulture); TextBox2.Text = rectangle.Height.ToString(CultureInfo.InvariantCulture); TextBoxCoordX.Text = rectangle.Position.CoordX.ToString(CultureInfo.InvariantCulture); TextBoxCoordY.Text = rectangle.Position.CoordY.ToString(CultureInfo.InvariantCulture); int colorRecIndex; switch (rectangle.Color.Title) { case "Red": colorRecIndex = 0; break; case "Green": colorRecIndex = 1; break; default: colorRecIndex = 2; break; } ColorList.SelectedIndex = colorRecIndex; break; } } }
/// <summary> /// Выполняет обновление рабочей области /// </summary> private void RefreshAll() { using (var gc = new GeometryContext()) { // очищаем канвас FieldCanvas.Children.Clear(); // создаем новый лист/список с фигурами var shapeInfoList = new List<ShapeInfo>(); // обрабатываем круги foreach (var circle in gc.Circles) { // добавляем в DataGrid shapeInfoList.Add(new ShapeInfo( circle.Id, ShapeInfo.ShapeType.Circle, circle.GetInfo(), circle.Position.ToString(), circle.Color.Title )); // рисуем фигуру Ellipse ellipse = new Ellipse { Height = circle.Radius*2, Width = circle.Radius*2, Fill = circle.Color.GetBrush() }; // устанавливаем на канвасе с указанными координатами ellipse.SetValue(Canvas.LeftProperty, circle.Position.CoordX); ellipse.SetValue(Canvas.TopProperty, circle.Position.CoordY); FieldCanvas.Children.Add(ellipse); } // обрабатываем прямоугольники foreach (var rec in gc.Rectangles) { // добавляем в DataGrid shapeInfoList.Add(new ShapeInfo( rec.Id, ShapeInfo.ShapeType.Rectangle, rec.GetInfo(), rec.Position.ToString(), rec.Color.Title )); // рисуем фигуру Rectangle recShape = new Rectangle { Width = rec.Width, Height = rec.Height, Fill = rec.Color.GetBrush() }; // устанавливаем на канвасе с указанными координатами recShape.SetValue(Canvas.LeftProperty, rec.Position.CoordX); recShape.SetValue(Canvas.TopProperty, rec.Position.CoordY); FieldCanvas.Children.Add(recShape); } _activeShape = null; ButtonDelete.IsEnabled = false; ButtonEdit.IsEnabled = false; // Закидываем в DataGrid информацию о всех фигурах DataView.ItemsSource = shapeInfoList; } }