在项目中可能用到如下表格式结构: DataGrid绑定好象没有此功能,因此自己定义了一个MyGrid代码如下: 自己定义一个UserControl,在其中添加一人Grid控件然后设置行和列如下: UserControlx:Class= Hahaman.UI.MyGrid xmlns= http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x= http://schemas.microsoft.c

  在项目中可能用到如下表格式结构:

  

  DataGrid绑定好象没有此功能,因此自己定义了一个MyGrid代码如下:

  自己定义一个UserControl,在其中添加一人Grid控件然后设置行和列如下:

<UserControl x:Class="Hahaman.UI.MyGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="88" d:DesignWidth="566">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="40*" />
            <RowDefinition Height="40*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />            
            <ColumnDefinition Width="10*" />
        </Grid.ColumnDefinitions>
    </Grid>
</UserControl>

  在控件代码中添加三个属性:

  public Dictionary Rects 保存矩形信息集合

  public Dictionary Texts 保存TextBlock控件集合

  public int Cols 保存列数

  

  添加第一列矩形框的代码:

Rectangle r1;
            r1= new Rectangle();
            r1.SetValue(Grid.RowSpanProperty, 2);
            r1.SetValue(Grid.ColumnProperty, 0);
            r1.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            r1.StrokeThickness = 1;
            LayoutRoot.Children.Add(r1);

  添加第一列文本框的代码:

TextBlock txt = new TextBlock();
            txt.SetValue(Grid.RowSpanProperty, 2);
            txt.SetValue(Grid.ColumnProperty, 0);
            txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            Texts.Add("0,0", txt);
            LayoutRoot.Children.Add(txt);

  添加其它的列:

void AddLine()
        {
            Rectangle r1;
            TextBlock txt;
            int n = LayoutRoot.ColumnDefinitions.Count - 1;
            for (int i = 1; i <=n; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    r1 = new Rectangle();
                    r1.SetValue(Grid.RowProperty, j);
                    r1.SetValue(Grid.ColumnProperty, i);
                    r1.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                    r1.Margin = new Thickness(i > 0 ? -1 : 0, j > 0 ? -1 : 0, 0, 0);
                    r1.StrokeThickness = 1;
                    LayoutRoot.Children.Add(r1);
                    Rects.Add(i + "," + j, r1);

txt = new TextBlock();
                    txt.SetValue(Grid.RowProperty, j);
                    txt.SetValue(Grid.ColumnProperty, i);
                    txt.Margin = new Thickness(3,0,0,0);
                    txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    LayoutRoot.Children.Add(txt);
                    Texts.Add(i+ "," + j, txt);
                }
            }
        }

  当Cols改变时需要重新绘制:

public int Cols
        {
            get
            {
                return LayoutRoot.ColumnDefinitions.Count - 1;
            }
            set
            {
                var old=LayoutRoot.ColumnDefinitions.Count - 1;
                if (value > old)
                {
                    for (int i = old; i < value; i++)
                    {
                        LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength ( 10, GridUnitType.Star ) });
                        
                    }
                    
                }
                else
                {
                    for (int i = 0; i < old - value; i++)
                    {
                        LayoutRoot.ColumnDefinitions.RemoveAt(value);                        
                    }

}
                ReDraw();
            }
        }

  这样设计时修改列数时就可以自动更新列数,如下图:

  

  前台控制代码:

var s = new SolidColorBrush();
            s.SetValue(SolidColorBrush.ColorProperty,Colors.LightGray);
            myGrid1.Rects["4,0"].Fill = s;
            myGrid1.Rects["4,1"].Fill = s;
            myGrid1.Texts["0,0"].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            myGrid1.Texts["0,0"].Text = "data";
            myGrid1.Texts["1,0"].Text = "data1";
            myGrid1.Texts["1,1"].Text = "data2";
源码下载

  本文来自lijun7788的博客,原文地址:http://blog.csdn.net/lijun7788/article/details/8085017

转载于:https://www.cnblogs.com/LYunF/archive/2012/10/22/2733993.html

Silverlight 自定义表格 转相关推荐

  1. 创建Silverlight自定义启动画面

    每一款商业的Silverlight项目,为了体现项目个性化,都会有不同的界面设计,项目UI设计的第一步就是创建个性的自定义启动画面,本文将介绍如何创建Silverlight自定义启动画面,也就是经常说 ...

  2. IOS自定义表格UITableViewCell

    在UITableView中,自定义表格,最原始是继承UITableViewCell,然后通过写代码方式去搞,但是这个费事了. 1.在storyboard中 给一个ViewController的tabi ...

  3. R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:使用gtExtras包添加一个图,显示表中某一列中的数字、并自定义表格数据显示的主题格式、并自定义数值数据的格式(例如百分比)

    R语言使用gt包和gtExtras包优雅地.漂亮地显示表格数据:使用gtExtras包添加一个图,显示表中某一列中的数字.并自定义表格数据显示的主题格式.并自定义数值数据的格式(例如百分比) 目录

  4. html生成自定义表格,自定义js的表格插件

    场景:指定元素,生成自定义表格. 目的:了解js的插件开发. html代码: 自定义表格插件 var test = new MyTable({ elid:"mytable",//定 ...

  5. vue自定义表格(每一列表格下面包含一个子表格)

    vue自定义表格(每一列表格下面包含一个子表格) 实现效果图 template <div class="marsTable" ref="marsTable" ...

  6. table自定义表格样式

    前言: 自定义表格样式,有一些自定义的需求,整理成通用样式就可以复用了 1,table表格每一列可以自定义宽度 2,table表格每一列中的每一项超出宽度不换行并显示省略号... <!DOCTY ...

  7. table自定义表格的封装

    前言: 对原生的table进行封装,让他满足我们一行显示不同个的需求. 实现效果:如图所示,一行显示不同数量的内容  实现代码: 1.封装的组件:custom_table.vue,源码看下面 (1)一 ...

  8. C# 将Excel转为PDF时自定义表格纸张大小

    目录 引入dll 1. 通过NuGet安装dll(2种方法) 2. 手动添加dll引用 代码及转换设置 注意事项 通过后端程序将Excel表格转为PDF格式时,直接转换后的PDF效果可能出现表格页面过 ...

  9. ElementUI自定义表格多选表头

    ElementUI自定义表格多选表头 首先开局一张图 需求说明,如图所示,表格中的每一行是一个复选框,点击表头的复选框,需要吧每一行中的复选框选中,当行中的复选框全部选中时自动勾选上表头的. 在UI框 ...

最新文章

  1. python免费入门教程-python小白如何入门,第一步要做什么?(附带免费教程)
  2. 8、非空约束(NOT NULL)
  3. windwon安装macaca环境
  4. linux ssd硬盘做缓存,linux系统中ssd当块设备缓存
  5. Rust : 为什么没有lifetime是不行的?
  6. VS2019、VS2017、VS2013产品密钥
  7. 3D建模初学者必看!你想要的学习工具都在这了!
  8. mysql获取autoincrement_MySQL获取Auto_increment字段刚插入的值 | 学步园
  9. html怎么设置冻结表格,如何在HTML表格上复制“冻结窗格”功能?
  10. 2023年全国最新工会考试精选真题及答案32
  11. K8S二进制环境搭建苹果电脑(M1芯片)
  12. Fabric 节点类型&交易流程
  13. 计算机怎样输入毫米符号,Excel 输入特殊符号的6种方法-excel技巧-电脑技巧收藏家...
  14. 谷歌中国发布年度热榜 iPhone成全球最流行词
  15. 【开源物联网】MQTT物联网网关Broker与Java开源实现
  16. 远控免杀从入门到实践(5)-代码篇-Python
  17. 51单片机ZFM60XSA光学指纹模块C语言应用
  18. Java面向对象和封装
  19. 爱普生Epson WF-6093 打印机驱动
  20. 小学计算机课教学工作总结,小学信息技术课教师工作总结3篇

热门文章

  1. C++ STL学习笔记(2) 容器结构与分类
  2. VGGNet原理及tensorflow实现
  3. 在simulink中建立调度增益控制系统的模型
  4. python调整图像大小_使用Python调整图像大小
  5. BUG解决:RuntimeError:Given groups=1,weight of size...expected input...but got 3 channels instead.
  6. PyCharm 中选中一个变量/函数后,所有用到这个变量/函数的地方高亮显示,改配色方案
  7. 港股区块链概念股走强,火币科技涨超17%
  8. SAP License:统计指标固定值和总值含义
  9. SAP License:SAP问题二则
  10. SAP License:SAP中的PRC增值税概念