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);

posted on   陆晓峰  阅读(2632)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2011年1月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示