Silverlight中用后台代码自定义Grid

通过后台代码来自定义一个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);
原文地址:https://www.cnblogs.com/Gyoung/p/2805736.html