首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

silverlight 自定义报表

2012-11-07 
silverlight 自定义表格在项目中可能用到如下表格式结构:DataGrid绑定好象没有此功能,因此自己定义了一个M

silverlight 自定义表格

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

silverlight 自定义报表

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<string, Rectangle> Rects 保存矩形信息集合

public Dictionary<string, TextBlock> Texts  保存TextBlock控件集合
public int Cols 保存列数

silverlight 自定义报表

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

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

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

silverlight 自定义报表
前台控制代码:

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

代码下载路径:http://download.csdn.net/detail/lijun7788/4659651

热点排行