Grid自动添加行

#region 自动添加行
        private void InitRows(int rowCount, Grid g)
        {
            while (rowCount-- > 0)
            {
                RowDefinition rd = new RowDefinition();
                rd.Height = new GridLength(60);
                g.RowDefinitions.Add(rd);
            }
        }
        private void InitColumns(int colCount, Grid g)
        {
            while (colCount-- > 0)
            {
                ColumnDefinition rd = new ColumnDefinition();
                rd.Width = new GridLength();
                g.ColumnDefinitions.Add(rd);
            }
        }
        string stringF4 = "{0:F4}";
        //列数为4列
        private void Init(Grid g, int rowcount)
        {
            InitRows(rowcount, g);
            InitColumns(4, g);
            #region
            for (int r = 1; r <= rowcount; r++)
            {
                if (r < rowcount)
                {
                    #region
                    for (int c = 0; c <= 4; c++)
                    {
                        Fi_VoucherChild child = new Fi_VoucherChild();
                        if (c == 0)
                        {
                            TextBoxLink text = new TextBoxLink();
                            text.Name = "text" + r + c;
                            text.FontSize = 14;
                            text.Height = g.Height;
                            text.LinkText = "摘要";
                            text.LinkClick += text_LinkClick;
                            Binding txtbinding = new Binding("Abstract");
                            txtbinding.StringFormat = stringF4;
                            text.SetBinding(TextBox.TextProperty, txtbinding);
                            g.Children.Add(text);
                            Grid.SetColumn(text, c);
                            Grid.SetRow(text, r);
                        }
                        if (c == 1)
                        {
                            ComboBoxLink text = new ComboBoxLink();
                            text.Height = g.Height;
                            text.FontSize = 14;
                            text.Name = "text" + r + c;
                            text.LinkText = "科目";
                            text.LinkClick += textCol_LinkClick;
                            text.ComboBoxLinkGotFocus += text_GotFocus;

                            g.Children.Add(text);
                            Grid.SetColumn(text, c);
                            Grid.SetRow(text, r);

                        }
                        if (c == 2)
                        {

                            TextBox text = new TextBox();
                            text.Height = g.Height;
                            text.FontSize = 28;
                            text.Name = "text" + r + c;
                            text.SpellCheck.IsEnabled = true;
                            text.TextAlignment = System.Windows.TextAlignment.Right;
                            text.LostFocus += text_LostFocus;
                            g.Children.Add(text);
                            Grid.SetColumn(text, c);
                            Grid.SetRow(text, r);
                        }
                        if (c == 3)
                        {
                            TextBox text = new TextBox();
                            text.FontSize = 28;
                            text.Height = g.Height;
                            text.TextAlignment = System.Windows.TextAlignment.Right;
                            text.LostFocus += textT_LostFocus;
                            text.Name = "text" + r + c;
                            g.Children.Add(text);
                            Grid.SetColumn(text, c);
                            Grid.SetRow(text, r);
                        }
                    }
                    #endregion
                }
                else
                {
                    for (int c = 0; c < 4; c++)
                    {
                        if (c == 0)
                        {
                            TextBlock tbkSum = new TextBlock();
                            tbkSum.Name = "tbkSum" + r + c;
                            tbkSum.Text = "合计:";
                            tbkSum.FontSize = 24;
                            g.Children.Add(tbkSum);
                            Grid.SetRow(tbkSum, r);
                            Grid.SetColumnSpan(tbkSum, c + 2);
                        }
                        if (c != 0 && c != 1)
                        {
                            TextBlock tbkSum = new TextBlock();
                            tbkSum.Name = "tbkSum" + r + c;
                            tbkSum.FontSize = 24;
                            tbkSum.TextAlignment = System.Windows.TextAlignment.Right;
                            g.Children.Add(tbkSum);
                            Grid.SetColumn(tbkSum, c);
                            Grid.SetRow(tbkSum, r);
                        }
                    }
                }

            }
            DataGrid dg = new DataGrid();

            this.UpdateLayout();
            #endregion
        }

        private void AddGrid(Grid g, int rowcount)
        {
            int rows = g.RowDefinitions.Count;
            InitRows(rowcount, g);
            InitColumns(4, g);
            #region
            for (int r = 1; r <= rowcount; r++)
            {
                #region
                for (int c = 0; c <= 4; c++)
                {
                    Fi_VoucherChild child = new Fi_VoucherChild();
                    if (c == 0)
                    {
                        TextBoxLink text = new TextBoxLink();
                        text.Name = "text" + r + c;
                        text.FontSize = 14;
                        text.Height = g.Height;
                        text.LinkText = "摘要";
                        text.LinkClick += text_LinkClick;
                        Binding txtbinding = new Binding("Abstract");
                        txtbinding.StringFormat = stringF4;
                        text.SetBinding(TextBox.TextProperty, txtbinding);
                        g.Children.Add(text);
                        Grid.SetColumn(text, c);
                        Grid.SetRow(text, rows-1);
                    }
                    if (c == 1)
                    {
                        ComboBoxLink text = new ComboBoxLink();
                        text.Height = g.Height;
                        text.FontSize = 14;
                        text.Name = "text" + r + c;
                        text.LinkText = "科目";
                        text.LinkClick += textCol_LinkClick;
                        text.ComboBoxLinkGotFocus += text_GotFocus;

                        g.Children.Add(text);
                        Grid.SetColumn(text, c);
                        Grid.SetRow(text, rows-1);

                    }
                    if (c == 2)
                    {

                        TextBox text = new TextBox();
                        text.Height = g.Height;
                        text.FontSize = 28;
                        text.Name = "text" + r + c;
                        text.SpellCheck.IsEnabled = true;
                        text.TextAlignment = System.Windows.TextAlignment.Right;
                        text.LostFocus += text_LostFocus;
                        g.Children.Add(text);
                        Grid.SetColumn(text, c);
                        Grid.SetRow(text, rows-1);
                    }
                    if (c == 3)
                    {
                        TextBox text = new TextBox();
                        text.FontSize = 28;
                        text.Height = g.Height;
                        text.TextAlignment = System.Windows.TextAlignment.Right;
                        text.LostFocus += textT_LostFocus;
                        text.Name = "text" + r + c;
                        g.Children.Add(text);
                        Grid.SetColumn(text, c);
                        Grid.SetRow(text, rows-1);
                    }
                }
                #endregion
            }
            DataGrid dg = new DataGrid();

            this.UpdateLayout();
            #endregion
        }
        #endregion

 

转载于:https://www.cnblogs.com/happinesshappy/p/5018312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值