在WPF中如何在画布中自由绘制大量形状集合而不使用网格

slhcrj9b  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(158)

我有一个需求,允许用户点击画布,每次创建一个矩形和8个文本框,输入矩形的尺寸,这些框会相对于矩形边框进行定位,所以这里会有一个List数组,其中列表中的每一项都是这些盒子的集合。画布可以让我正常地画一个矩形。我发现动态收集这些数据的唯一合适方法是“网格”,如下所示:

  1. var g1 = new Grid();
  2. g1.Width = 100;
  3. g1.Height = 100;
  4. Canvas.SetLeft(g1, 10);
  5. Canvas.SetTop(g1, 10);
  6. Rectangle rectangle = new Rectangle
  7. {
  8. Width = 20,
  9. Height = 30,
  10. Fill = red,
  11. StrokeThickness = 2,
  12. Stroke = Brushes.Black
  13. };
  14. Canvas.SetLeft(rectangle, 10);
  15. Canvas.SetTop(rectangle, 10);
  16. g.Children.Add(rectangle);
  17. LayoutCanvas.Children.Add(g);

但是如果我现在需要添加盒子,所有盒子都会转到网格的中心。

  1. var txt = new TextBox();
  2. txt.Text = "New Text1";
  3. txt.Width = 30;
  4. txt.Height = 30;
  5. Canvas.SetLeft(txt, 20);
  6. Canvas.SetTop(txt, 30);
  7. g1.Children.Add(txt);

为了解决这个问题,我必须用途:

  1. g1.ColumnDefinitions.Add(new ColumnDefinition());
  2. g1.RowDefinitions.Add(new RowDefinition());
  3. g1.RowDefinitions.Add(new RowDefinition());
  4. g1.RowDefinitions.Add(new RowDefinition());

但我的应用程序不需要网格中的行和列的这种组织,需要它是完全形状和完全动态的,最好甚至没有XAML。我尝试了其他对象,如StackPanel,但它们都没有太大的区别。
解决方案:我现在有这样的代码:

  1. Grid DynamicGrid = new Grid();
  2. DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
  3. DynamicGrid.VerticalAlignment = VerticalAlignment.Stretch;
  4. DynamicGrid.ShowGridLines = true;
  5. DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);
  6. var c1 = new Canvas();
  7. var c2 = new Canvas();
  8. //--------------------- Canvas1 Children --------------------->
  9. Rectangle c1Rectangle = new Rectangle
  10. {
  11. Width = 100,
  12. Height = 100,
  13. Fill = new SolidColorBrush(Color.FromRgb(255, 0, 0)),
  14. StrokeThickness = 2,
  15. Stroke = Brushes.Black
  16. };
  17. Canvas.SetLeft(c1Rectangle, 0);
  18. Canvas.SetTop(c1Rectangle, 0);
  19. TextBlock c1Textbox = new TextBlock();
  20. c1Textbox.Width = 50;
  21. c1Textbox.Height = 50;
  22. c1Textbox.Text = "Text1";
  23. c1Textbox.FontSize = 14;
  24. c1Textbox.FontWeight = FontWeights.Bold;
  25. c1Textbox.Foreground = new SolidColorBrush(Colors.White);
  26. c1Textbox.VerticalAlignment = VerticalAlignment.Top;
  27. Canvas.SetLeft(c1Textbox, 10);
  28. Canvas.SetTop(c1Textbox, 10);
  29. //--------------------- Canvas2 Children --------------------->
  30. Rectangle c2Rectangle = new Rectangle
  31. {
  32. Width = 100,
  33. Height = 100,
  34. Fill = new SolidColorBrush(Colors.LightBlue),
  35. StrokeThickness = 2,
  36. Stroke = Brushes.Red
  37. };
  38. Canvas.SetLeft(c2Rectangle, 420);
  39. Canvas.SetTop(c2Rectangle, 220);
  40. TextBlock c2Textbox = new TextBlock();
  41. c2Textbox.Width = 50;
  42. c2Textbox.Height = 50;
  43. c2Textbox.Text = "Text2";
  44. c2Textbox.FontSize = 14;
  45. c2Textbox.FontWeight = FontWeights.Bold;
  46. c2Textbox.Foreground = new SolidColorBrush(Colors.Black);
  47. c2Textbox.VerticalAlignment = VerticalAlignment.Top;
  48. Canvas.SetLeft(c2Textbox, 440);
  49. Canvas.SetTop(c2Textbox, 240);
  50. c1.Children.Add(c1Rectangle);
  51. c1.Children.Add(c1Textbox);
  52. c2.Children.Add(c2Rectangle);
  53. c2.Children.Add(c2Textbox);
  54. DynamicGrid.Children.Add(c1);
  55. DynamicGrid.Children.Add(c2);
  56. this.Content = DynamicGrid;
  57. this.Show();
66bbxpm5

66bbxpm51#

尝试为每个对象创建一个画布,这应该允许您自由地定位每个对象:

  1. Rectangle rectangle = new Rectangle
  2. {
  3. Width = 20,
  4. Height = 30,
  5. Fill = red,
  6. StrokeThickness = 2,
  7. Stroke = Brushes.Black
  8. };
  9. var txt = new TextBox(){
  10. Text = "New Text1",
  11. Width = 30,
  12. Height = 30,
  13. };
  14. g1 = new Grid()
  15. {
  16. Width = 100,
  17. Height = 100,
  18. Children =
  19. {
  20. new Canvas() { Children = { txt} },
  21. new Canvas() { Children = { rectangle} }
  22. }
  23. };
  24. Canvas.SetLeft(txt, 20);
  25. Canvas.SetTop(txt, 30);
  26. Canvas.SetLeft(rectangle, 10);
  27. Canvas.SetTop(rectangle, 10);
展开查看全部

相关问题