Silverlight 自定义表格 转

在项目中可能用到如下表格式结构: 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

你可能感兴趣的:(silverlight)