Silverlight 自定义表格

功能很简单,用于备忘

public class Table:Grid
    {
        #region Table属性

        private int rows;
        private int columns;
        private Color tableColor;

        public int Rows {
            get { return rows; }
            set { rows = value; }
        }

        public int Columns {
            get { return columns; }
            set { columns = value; }
        }

        public Color TableColor {
            get { return tableColor; }
            set { tableColor = value; }
        }

        #endregion


        #region Table事件

        #endregion

        #region Table方法

        public Table()
        {

        }

        public Table(int row, int couumn,Color color)
        {
            rows = row;
            columns = couumn;
            tableColor = color;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void initTable() {

            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.Black);
            border.BorderThickness = new Thickness(2);
            Grid grid = new Grid();

            for (int i = 0; i < rows; i++)
            {
                grid.RowDefinitions.Insert(i, new RowDefinition() { Height = new GridLength(1,GridUnitType.Star) });
            }
            for (int j = 0; j < columns; j++)
            {
                grid.ColumnDefinitions.Insert(j, new ColumnDefinition() { Width = new GridLength(1,GridUnitType.Star) });
            }

            //添加矩阵到单元格中
            for (int r = 0; r < rows; r++)
            {
                for (int c = 0; c < columns; c++) {
                    Rectangle rec1 = getRectangle();
                    rec1.SetValue(Grid.RowProperty, r);
                    rec1.SetValue(Grid.ColumnProperty, c);
                    grid.Children.Add(rec1);
                }
            }
            border.Child = grid;
            this.Children.Add(border);
        }

        Rectangle getRectangle()
        {
            Rectangle rectangle = new Rectangle
            {
                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
                Margin = new Thickness(0, 0, 0, 0),
                Stroke = new SolidColorBrush(Colors.Black),
            };

            return rectangle;
        }

        Rectangle getRectangle(int r,int c,Color color)
        {
            Rectangle rectangle = new Rectangle
            {
                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
                Margin = new Thickness(0, 0, 0, 0),
                Stroke = new SolidColorBrush(color),
            };

            return rectangle;
        }

        #endregion
       
    }

使用方法

Table table = new Table();
                table.Rows = int.Parse(tbRow.Text.Trim());
                table.Columns = int.Parse(tbColumn.Text.Trim());
                table.initTable();
                gridContent.Children.Add(table);

转载于:https://www.cnblogs.com/luxiaofeng54/archive/2011/01/02/1924175.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值