通过后台代码来自定义一个Grid

//实例化一个Grid
            Grid grid = new Grid();
            //设置RowDefinition
            RowDefinition row1 = new RowDefinition();
            row1.Height = new GridLength(20);
            grid.RowDefinitions.Add(row1);
            RowDefinition row2 = new RowDefinition();
            row2.Height = new GridLength(80);
            grid.RowDefinitions.Add(row2);

            Button b = new Button();
            b.Height = 20;
            b.Width = 30;
            b.HorizontalAlignment = HorizontalAlignment.Right;
            b.Content = "关闭";
            b.Click += b_Click;
            //将Button添加到Grid中
            grid.Children.Add(b);
            //设置Button在Grid中的位置
            b.SetValue(Grid.RowProperty, 0);

            TextBlock txt = new TextBlock();
            txt.Text = "this is a test";
            txt.Height = 80;
            txt.Width = 200;
            //将TextBlock添加到Grid中
            grid.Children.Add(txt);
            //设置TextBlock在Grid中的位置
            txt.SetValue(Grid.RowProperty, 1);

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-03-31
  • 2021-12-22
  • 2021-05-27
  • 2021-09-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
  • 2022-02-24
  • 2022-12-23
  • 2021-06-08
  • 2022-12-23
相关资源
相似解决方案