用Grid写DataGrid控件

用Grid写DataGrid控件,第1张

概述     不少人提到我这篇文章没有demo,现将demo提供如下:demo下载地址  该demo可以直接运行,演示了最基本的数据呈现及合并单元格.     在ASP.NET中微软官方提供的GridView控件能实现各种样式,如设置字体、背景色、合并单元格等等。这些控件无非都是基本控件的封装,所有样式都是通过控制子控件样式来实现。     在silverlight中有基本的布局控件Grid、显示数据

     不少人提到我这篇文章没有demo,现将demo提供如下:demo下载地址  该demo可以直接运行,演示了最基本的数据呈现及合并单元格.

    在ASP.NET中微软官方提供的GrIDVIEw控件能实现各种样式,如设置字体、背景色、合并单元格等等。这些控件无非都是基本控件的封装,所有样式都是通过控制子控件样式来实现。

    在silverlight中有基本的布局控件GrID、显示数据的TextBlock。有了这些基本控件即可写出能符合各种要求的控件来。

    在编写控件时会遇到如下麻烦:

    1、控件边框问题。GrID本身确实有线,只要将Showlines设置为true即可,但是这个线却是虚线,不能设置样式。

    2、单元格单击事件问题。GrID中的单元格只有点中内容时才有效,比如单元格比较大就一个字的情况,只有点中这个字时才能触发事件。

    3、合并单元格问题。合并单元格的思路是先将被合并的单元格内容清楚,再将合并的内容扩展,思路比较清晰但实现上可能会遇到麻烦。

    重新分析设计思路,其实这些问题并不难解决。

    1、边框问题,那可以用一个Rectangle来顶替。但因为每个单元格都有边框,且合并但远格时边框也要随着伸缩。于是可以考虑动态画这个Rectangle,创建一个单元格就画一个Rectangle.其中代码如下:

public voID DrawRectangle(color linecolor)        {            Rectangle r = new Rectangle();            r.stroke = new SolIDcolorBrush(linecolor);            //r.Fill = backGround;            r.strokeThickness = lineBord;            //r.Stretch = Stretch.Fill;            //Canvas.SetZIndex(r,-1);            r.margin = new Thickness(0,-1,-1);            if ((Y == columnCount - 1) && (columnCount != 0))            {                r.margin = new Thickness(0,-1);            }            this.LayoutRoot.Children.Add(r);            //CellRectangle = r;        }

这个方法是公有方法,是在声明单元格后调用的。

    2、单元格单击事件问题就可以想方设法让单元格内容完全填充GrID的整个单元格,于是可以将每个单元格看做是一个独立的子控件,也就是说先封装一个单元格控件,该控件能设置各种样式,如背景色、字体大小、还能当模板用。 该单元格控件名称为:   SimpleDataGrIDCell

    3、合并单元格的思路很简单,但是难点是怎么找到这个单元格,考虑到每个单元格都是一个独立的自定义控件,故而可以给这个单元格设置两个坐标属性X和Y,然后在GrID中查找该坐标的单元格即可,同时合并单元格的属性应该放在单元格中才行,但实现单元格的合并是在调用单元格的主控件中实现,这时可以用事件来解决这个问题。即在单元格中声明两个事件。

/// <summary>        /// 合并行        /// </summary>        public event Action<SimpleDataGrIDCell> SetrowspanEvent;        /// <summary>        /// 合并列        /// </summary>        public event Action<SimpleDataGrIDCell> SetColumnSpanEvent;

在设置合并单元格时进行调用

public int ColumnSpan        {            get { return columnSpan; }            set            {                columnSpan = value;                if (SetColumnSpanEvent != null)                {                    SetColumnSpanEvent(this);                }            }        }        public int rowspan        {            get { return rowspan; }            set            {                rowspan = value;                if (SetrowspanEvent != null)                {                    SetrowspanEvent(this);                }            }        }

在声明单元格时注册这两个事件

    cell.SetrowspanEvent += new Action<SimpleDataGrIDCell>(cell_SetrowspanEvent);    cell.SetColumnSpanEvent += new Action<SimpleDataGrIDCell>(cell_SetColumnSpanEvent);
这些问题都解决了那其他都迎刃而解。下面是整个控件的源码:单元格的设计界面: SimpleDataGrIDCell.xaml
<UserControl x:Class="PAS.PerfCmcc.SimpleDataGrID.SimpleDataGrIDCell"    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" MouseleftbuttonUp="UserControl_MouseleftbuttonUp" MouseRightbuttonUp="UserControl_MouseRightbuttonUp" MouseEnter="UserControl_MouseEnter">        <GrID x:name="LayoutRoot" Background="transparent">        <ContentPresenter x:name="cpContent" Canvas.ZIndex="99"></ContentPresenter>    </GrID></UserControl>
C#代码:
using System;using System.Collections.Generic;using System.linq;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;using System.ComponentModel;namespace PAS.PerfCmcc.SimpleDataGrID{    public partial class SimpleDataGrIDCell : UserControl    {        public SimpleDataGrIDCell()        {            InitializeComponent();            this.LayoutRoot.Loaded += new RoutedEventHandler(LayoutRoot_Loaded);            this.MouseEnter += new MouseEventHandler(SimpleDataGrIDCell_MouseEnter);            this.MouseLeave += new MouseEventHandler(SimpleDataGrIDCell_MouseLeave);            Rectangle.IsHitTestVisible = false;            Rectangle.Fill = new SolIDcolorBrush(color.FromArgb(0x22,0x00,0x00));            Rectangle.strokeThickness = 1;            Rectangle.stroke = new SolIDcolorBrush(colors.lightGray);            Rectangle.RadiusX = 2;            Rectangle.RadiusY = 2;            Rectangle.margin = new Thickness(1);            Rectangle.Visibility = System.windows.Visibility.Collapsed;            this.LayoutRoot.Children.Add(Rectangle);        }        private Rectangle Rectangle = new Rectangle();        voID SimpleDataGrIDCell_MouseLeave(object sender,MouseEventArgs e)        {            Rectangle.Visibility = System.windows.Visibility.Collapsed;        }        voID SimpleDataGrIDCell_MouseEnter(object sender,MouseEventArgs e)        {            Rectangle.Visibility = System.windows.Visibility.Visible;        }        voID LayoutRoot_Loaded(object sender,RoutedEventArgs e)        {            InitControls();        }        #region 私有字段        private Brush backGround = new SolIDcolorBrush(colors.White);        private Brush columnBackground = new SolIDcolorBrush(colors.White);        private Brush headerBackground = new SolIDcolorBrush(colors.White);        private color forecolor = colors.Black;        private int columnSpan = 1;        private int rowspan = 1;        private Object content = new TextBlock() { Text="显示数据"};        private HorizontalAlignment hAlign = HorizontalAlignment.Center;        private VerticalAlignment vAlign = VerticalAlignment.Center;        private Rectangle rectangle;        private double lineBord = 1;        #endregion        #region 单元格属性        public int X { get; set; }        public int Y { get; set; }        public int rowCount { get; set; }        public int columnCount { get; set; }        /// <summary>        /// 表头样式        /// </summary>        public Brush headerBackground        {            get { return this.headerBackground; }            set            {                headerBackground = value;                this.Background = value;            }        }        /// <summary>        /// 一列的背景色        /// </summary>        public Brush ColumnBackground        {            get { return this.columnBackground; }            set            {                columnBackground = value;                this.Background = value;            }        }        [DefaultValue("1"),Description("边框宽度"),category("Appearance")]        public double lineBord        {            get { return lineBord; }            set { lineBord = value; }        }        public static new DependencyProperty BackgroundProperty =            DependencyProperty.Register("Background",typeof(Brush),typeof(SimpleDataGrIDCell),null);        public new Brush Background        {            //get { return backGround; }            get { return (Brush)GetValue(BackgroundProperty); }            set            {                backGround = value;                SetValue(BackgroundProperty,value);                this.LayoutRoot.Background = value;            }        }        public static DependencyProperty ForecolorProperty =            DependencyProperty.Register("Forecolor",typeof(color),null);        public color Forecolor        {            get { return forecolor; }            set            {                forecolor = value;                this.Foreground = new SolIDcolorBrush(forecolor);            }        }        public int ColumnSpan        {            get { return columnSpan; }            set            {                columnSpan = value;                if (SetColumnSpanEvent != null)                {                    SetColumnSpanEvent(this);                }            }        }        public int rowspan        {            get { return rowspan; }            set            {                rowspan = value;                if (SetrowspanEvent != null)                {                    SetrowspanEvent(this);                }            }        }        public new Object Content        {            get { return content; }            set            {                content = value;                this.cpContent.Content = content;            }        }        public new Object Tag { get; set; }        public new HorizontalAlignment HorizontalAlignment        {            get { return hAlign; }            set            {                hAlign = value;                this.cpContent.HorizontalAlignment = hAlign;            }        }        public new VerticalAlignment VerticalAlignment        {            get { return vAlign; }            set            {                vAlign = value;                this.cpContent.VerticalAlignment = vAlign;            }        }        public Rectangle CellRectangle        {            get { return rectangle; }            set            {                rectangle = value;                if (rectangle != null)                {                    rectangle.margin = new Thickness(0,-1);                    this.LayoutRoot.Children.Add(rectangle);                }            }        }        #endregion        public voID DrawRectangle(color linecolor)        {            Rectangle r = new Rectangle();            r.stroke = new SolIDcolorBrush(linecolor);            //r.Fill = backGround;            r.strokeThickness = lineBord;            //r.Stretch = Stretch.Fill;            //Canvas.SetZIndex(r,-1);            }            this.LayoutRoot.Children.Add(r);            //CellRectangle = r;        }        private voID InitControls()        {            this.cpContent.VerticalAlignment = vAlign;            this.cpContent.HorizontalAlignment = hAlign;        }        /// <summary>        /// 合并行        /// </summary>        public event Action<SimpleDataGrIDCell> SetrowspanEvent;        /// <summary>        /// 合并列        /// </summary>        public event Action<SimpleDataGrIDCell> SetColumnSpanEvent;        /// <summary>        /// 单击单元格事件        /// </summary>        public event Action<SimpleDataGrIDCell> CellClick;        /// <summary>        /// 右键单元格事件        /// </summary>        public event Action<SimpleDataGrIDCell> CellRightmouseClick;        private voID UserControl_MouseleftbuttonUp(object sender,MousebuttonEventArgs e)        {            SimpleDataGrIDCell grIDCell = sender as SimpleDataGrIDCell;            if (CellClick != null)            {                CellClick(grIDCell);            }        }        private voID UserControl_MouseRightbuttonUp(object sender,MousebuttonEventArgs e)        {            SimpleDataGrIDCell grIDCell = sender as SimpleDataGrIDCell;            if (CellRightmouseClick != null)            {                CellRightmouseClick(grIDCell);            }        }        private voID UserControl_MouseEnter(object sender,MouseEventArgs e)        {        }    }}

 

主界面: SimpleDataGrID.xaml

 

<UserControl x:Class="PAS.PerfCmcc.SimpleDataGrID.SimpleDataGrID"    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">        <GrID x:name="LayoutRoot" Background="White">        <border borderBrush="Black" borderThickness="0" x:name="grIDBord">            <ScrollVIEwer HorizontalScrollbarVisibility="auto" VerticalScrollbarVisibility="auto">                 <GrID name="NewGrID" MouseleftbuttonUp="NewGrID_MouseleftbuttonUp" MouseRightbuttonUp="NewGrID_MouseRightbuttonUp"></GrID>            </ScrollVIEwer>        </border>    </GrID></UserControl>

 

实现代码:

using System;using System.Collections.Generic;using System.linq;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;using System.Runtime.Serialization.Json;using System.IO;using System.ComponentModel;namespace PAS.PerfCmcc.SimpleDataGrID{    public partial class SimpleDataGrID : UserControl    {        SimpleDataGrIDCell sdCell = new SimpleDataGrIDCell();        public SimpleDataGrID()        {            InitializeComponent();            this.LayoutRoot.Loaded += new RoutedEventHandler(LayoutRoot_Loaded);        }        public Brush DefaultCellBackground { get; set; }        voID sdCell_SetrowspanEvent(SimpleDataGrIDCell obj)        {            foreach (SimpleDataGrIDCell child in this.NewGrID.Children)            {                if (child == null) continue;                int rowIndex = (int)child.GetValue(GrID.RowProperty);                int colindex = (int)child.GetValue(GrID.ColumnProperty);                if (rowIndex == child.X && child.Y == colindex)                {                    int rowspan = child.rowspan;                    int meil = rowspan - 1;                    for (int i = 0; i < meil; i++)                    {                        SimpleDataGrIDCell ch = GetCell(i + rowIndex,colindex);                        ch = null;                    }                    GrID.Setrowspan(child,rowspan);                }            }        }        voID sdCell_SetColumnSpanEvent(SimpleDataGrIDCell obj)        {            foreach (SimpleDataGrIDCell child in this.NewGrID.Children)            {                if (child == null) continue;                int rowIndex = (int)child.GetValue(GrID.RowProperty);                int colindex = (int)child.GetValue(GrID.ColumnProperty);                if (rowIndex == child.X && child.Y == colindex)                {                    int colSpan = child.ColumnSpan;                    int meil = colSpan - 1;                    for (int i = 0; i < meil; i++)                    {                        SimpleDataGrIDCell ch = GetCell(rowIndex,colindex + i);                        ch = null;                    }                    GrID.Setrowspan(child,colSpan);                }            }        }        voID LayoutRoot_Loaded(object sender,RoutedEventArgs e)        {            //Inittable();        }        private int tempX = 0;        private int tempY = 0;        #region 控件字段        private Brush backGround = new SolIDcolorBrush(colors.White);        private Brush columnBackground = new SolIDcolorBrush(colors.White);        private Brush headerBackground = new SolIDcolorBrush(colors.White);        private string columnGroup = "0";        private int[] columnGroups = new int[] { 0 };        private int defaultRowHeight = 28;        private int rowCount = 3;        private int columnCount = 3;        private double lineBord = 1;        private color linecolor = colors.Black;        private color forecolor = colors.Black;        private Object dataSource;        #endregion        #region 控件属性        public static new DependencyProperty BackgroundProperty =            DependencyProperty.Register("Background",typeof(SimpleDataGrID),value);            }        }        /// <summary>        /// 表头样式        /// </summary>        public static DependencyProperty headerBackgroundProperty =            DependencyProperty.Register("headerBackground",null);        [browsable(true)]        [DefaultValue("1"),Description("表头颜色"),category("headerStyle")]        public Brush headerBackground        {            get { return (Brush)GetValue(headerBackgroundProperty); }            set            {                headerBackground = value;                SetValue(headerBackgroundProperty,value);            }        }        /// <summary>        /// 一列的背景色        /// </summary>        public static DependencyProperty ColumnBackgroundProperty =            DependencyProperty.Register("ColumnBackground",Description("列颜色"),category("ColumnStyle")]        public Brush ColumnBackground        {            get { return (Brush)GetValue(ColumnBackgroundProperty); }            set            {                columnBackground = value;                SetValue(ColumnBackgroundProperty,value);            }        }        public static DependencyProperty ColumnGroupsProperty =            DependencyProperty.Register("ColumnGroup",typeof(string),null);        [browsable(true)]        [DefaultValue("0"),Description("需要变换颜色的列"),category("ColumnStyle")]        public string ColumnGroups        {            get { return (string)GetValue(ColumnGroupsProperty); }            set            {                this.columnGroup = value;                SetValue(ColumnGroupsProperty,value);                string[] temp = columnGroup.Split(',');                int len = temp.Length;                columnGroups = new int[len];                for (int i = 0; i < len; i++)                {                    columnGroups[i] = Convert.ToInt32(temp[i]);                }            }        }        public static DependencyProperty ForecolorProperty =            DependencyProperty.Register("Forecolor",null);        public color Forecolor        {            get { return (color)GetValue(ForecolorProperty); }            set            {                forecolor = value;                SetValue(ForecolorProperty,value);            }        }        [browsable(true)]        [DefaultValue("1"),category("Appearance")]        public double lineBord        {            get { return lineBord; }            set { lineBord = value; }        }        /// <summary>        /// 默认行高        /// </summary>        public int DefaultRowHeight        {            get { return defaultRowHeight; }            set { defaultRowHeight = value; }        }        /// <summary>        /// 默认行数        /// </summary>        public int RowCount        {            get { return rowCount; }            set            {                rowCount = value;                setRowCount(rowCount);            }        }        /// <summary>        /// 默认列数        /// </summary>        public int ColumnCount        {            get { return columnCount; }            set            {                columnCount = value;                setColCount(columnCount);            }        }        /// <summary>        /// 边框颜色        /// </summary>        public static DependencyProperty linecolorProperty =            DependencyProperty.Register("linecolor",null);        [browsable(true)]        [DefaultValue("#FFFFFF"),Description("边框颜色"),category("Appearance")]        public color linecolor        {            //get { return linecolor; }            get { return (color)GetValue(linecolorProperty); }            set            {                SetValue(linecolorProperty,value);                linecolor = value;                Changebordercolor();            }        }        /// <summary>        /// 数据源        /// </summary>        public Object DataSource        {            get { return dataSource; }            set { dataSource = value; }        }        #endregion        public voID Load()        {            //Inittable();        }        /// <summary>        /// 设置行数        /// </summary>        /// <param name="count"></param>        private voID setRowCount(int count)        {            this.NewGrID.RowDeFinitions.Clear();            for (int i = 0; i < count; i++)            {                RowDeFinition row = new RowDeFinition();                row.Height = new GrIDLength(defaultRowHeight);                NewGrID.RowDeFinitions.Add(row);            }            SetGrIDStyle();            InitGrIDByEmptyCell(count,columnCount);        }        /// <summary>        /// 设置列数        /// </summary>        /// <param name="count"></param>        private voID setColCount(int count)        {            this.NewGrID.ColumnDeFinitions.Clear();            for (int i = 0; i < count; i++)            {                ColumnDeFinition col = new ColumnDeFinition();                this.NewGrID.ColumnDeFinitions.Add(col);            }            SetGrIDStyle();            InitGrIDByEmptyCell(rowCount,count);        }        /// <summary>        /// 设置边框颜色        /// </summary>        private voID setlinecolor()        {                    }        private voID SettableHeight()        {            this.NewGrID.Height = rowCount * defaultRowHeight;            this.grIDBord.Height = this.NewGrID.Height;        }        private voID Inittable()        {            setColCount(columnCount);            setRowCount(rowCount);            //SettableHeight();            InitContent();        }        private voID InitContent()        {            this.NewGrID.Children.Clear();            for (int i = 0; i < rowCount; i++)            {                for (int j = 0; j < columnCount; j++)                {                    tempX = i;                    tempY = j;                    //DrawRectangle(i,j);                    SimpleDataGrIDCell cell = new SimpleDataGrIDCell();                    if (DefaultCellBackground != null)                        cell.Background = DefaultCellBackground;                    cell.X = i;                    cell.Y = j;                    cell.ColumnSpan = 1;                    cell.rowspan = 1;                    TextBlock tb = new TextBlock();                    tb.Text = "test";                    cell.Content = tb;                    cell.Forecolor = colors.Black;                    // cell.Background = new SolIDcolorBrush(colors.White);                    cell.HorizontalAlignment = HorizontalAlignment.Center;                    if (i == 1 && j == 1)                    {                        cell.ColumnSpan = 2;                        j++;                    }                    cell.DrawRectangle(linecolor);                    this.NewGrID.Children.Add(cell);                    GrID.SetColumn(cell,tempY);                    GrID.SetRow(cell,tempX);                    GrID.Setrowspan(cell,cell.rowspan);                    GrID.SetColumnSpan(cell,cell.ColumnSpan);                }            }        }        /// <summary>        /// 改变边框颜色        /// </summary>        private voID Changebordercolor()        {            for (int i = 0; i < rowCount; i++)            {                for (int j = 0; j < columnCount; j++)                {                    SimpleDataGrIDCell cell = GetCell(i,j);                    if (cell != null)                    {                        cell.DrawRectangle(linecolor);                    }                }            }        }        /// <summary>        /// 初始化表格内容        /// </summary>        /// <param name="row"></param>        /// <param name="col"></param>        private voID InitGrIDByEmptyCell(int row,int col)        {            this.NewGrID.Children.Clear();            for (int i = 0; i < row; i++)            {                for (int j = 0; j < col; j++)                {                    tempX = i;                    tempY = j;                    SimpleDataGrIDCell cell = new SimpleDataGrIDCell();                    if (DefaultCellBackground != null)                        cell.Background = DefaultCellBackground;                    cell.SetrowspanEvent += new Action<SimpleDataGrIDCell>(cell_SetrowspanEvent);                    cell.SetColumnSpanEvent += new Action<SimpleDataGrIDCell>(cell_SetColumnSpanEvent);                    cell.rowCount = rowCount;                    cell.columnCount = columnCount;                    cell.X = i;                    cell.Y = j;                    //背景色的填充顺序:先填充背景色、再填充列背景色、最后填充表头颜色。一般情况如果是列背景色则需要先填充背景色在填充列背景色                    //如果是表头背景色则先填充背景色再填充表头背景色                    if (((SolIDcolorBrush)this.backGround).color.ToString() != colors.White.ToString())                    {                        cell.Background = this.backGround;//背景色                    }                    if (((SolIDcolorBrush)this.columnBackground).color.ToString() != (colors.White).ToString())                    {                        int cLen = columnGroups.Length;                        for (int t = 0; t < cLen; t++)                        {                            if (i != 0 && j == columnGroups[t])                            {                                cell.ColumnBackground = this.columnBackground;//列背景色                            }                        }                    }                    if (((SolIDcolorBrush)this.headerBackground).color.ToString() != (colors.White).ToString())                    {                        if (i == 0)                        {                            cell.headerBackground = this.headerBackground;//表头背景色                        }                    }                    if (this.forecolor.ToString() != (colors.Black).ToString())                    {                        cell.Forecolor = this.forecolor;                    }                    //cell.ColumnSpan = 1;                    //cell.rowspan = 1;                    cell.DrawRectangle(linecolor);                    this.NewGrID.Children.Add(cell);                    GrID.SetColumn(cell,tempX);                    //GrID.Setrowspan(cell,cell.rowspan);                    //GrID.SetColumnSpan(cell,cell.ColumnSpan);                }            }        }        voID cell_SetColumnSpanEvent(SimpleDataGrIDCell obj)        {            foreach (SimpleDataGrIDCell child in this.NewGrID.Children)            {                if (child == null) continue;                int rowIndex = (int)child.GetValue(GrID.RowProperty);                int colindex = (int)child.GetValue(GrID.ColumnProperty);                if (rowIndex == obj.X && obj.Y == colindex)                {                    int colSpan = child.ColumnSpan;                    int meil = colSpan - 1;                    for (int i = 0; i < meil; i++)                    {                        SimpleDataGrIDCell ch = GetCell(rowIndex,colindex + i + 1);                        if (ch == null) continue;                        ch.LayoutRoot.Children.Clear();                        ch.Background = new SolIDcolorBrush(colors.transparent);                        ch = null;                    }                    GrID.SetColumnSpan(child,colSpan);                }            }        }        voID cell_SetrowspanEvent(SimpleDataGrIDCell obj)        {            foreach (SimpleDataGrIDCell child in this.NewGrID.Children)            {                if (child == null) continue;                int rowIndex = (int)child.GetValue(GrID.RowProperty);                int colindex = (int)child.GetValue(GrID.ColumnProperty);                if (rowIndex == obj.X && obj.Y == colindex)                {                    int rowspan = child.rowspan;                    int meil = rowspan - 1;                    for (int i = 0; i < meil; i++)                    {                        SimpleDataGrIDCell ch = GetCell(i + rowIndex + 1,colindex);                        if (ch == null) continue;                        ch.LayoutRoot.Children.Clear();                        ch.Background = new SolIDcolorBrush(colors.transparent);                        ch = null;                    }                    GrID.Setrowspan(child,rowspan);                }            }        }        private voID DrawRectangle(int row,int col)        {            Rectangle r = new Rectangle();            r.stroke = new SolIDcolorBrush(linecolor);            r.strokeThickness = 0.5;            r.Stretch = Stretch.Fill;            this.NewGrID.Children.Add(r);            GrID.SetRow(r,row);            GrID.SetColumn(r,col);        }        public SimpleDataGrIDCell GetCell(int row,int col)        {            foreach (SimpleDataGrIDCell child in this.NewGrID.Children)            {                if (child == null) continue;                                int rowIndex = (int)child.GetValue(GrID.RowProperty);                int colindex = (int)child.GetValue(GrID.ColumnProperty);                if (rowIndex == row && col == colindex)                    return child;            }            return null;        }        /// <summary>        /// 设置行高        /// </summary>        /// <param name="row"></param>        /// <param name="height"></param>        public voID SetRowHeight(int row,int height)        {            NewGrID.RowDeFinitions[row].Height = new GrIDLength(height);            for (int i = 0; i < columnCount; i++)            {                SimpleDataGrIDCell cell = GetCell(row,i);                if (cell != null)                {                    if (height == 0)                    {                        cell.Visibility = Visibility.Collapsed;                    }                    else                    {                        cell.Visibility = Visibility.Visible;                    }                }            }        }        /// <summary>        /// 设置列宽        /// </summary>        /// <param name="col"></param>        /// <param name="wIDth"></param>        public voID SetColumnWIDth(int col,int wIDth)        {            NewGrID.ColumnDeFinitions[col].WIDth = new GrIDLength(wIDth);            for (int i = 0; i < columnCount; i++)            {                SimpleDataGrIDCell cell = GetCell(i,col);                if (cell != null)                {                    if (wIDth == 0)                    {                        cell.Visibility = Visibility.Collapsed;                    }                    else                    {                        cell.Visibility = Visibility.Visible;                    }                    if (cell.ColumnSpan > 1)                    {                        cell_SetColumnSpanEvent(cell);                    }                }            }        }        public voID SetGrIDStyle()        {            //边框颜色            Style style = Application.Current.Resources["SimpleGrIDStyle"] as Style;            this.Style = style;            //边框颜色            color lcolor = (color)this.GetValue(SimpleDataGrID.linecolorProperty);            this.linecolor = lcolor;            //前景色            lcolor = (color)this.GetValue(SimpleDataGrID.ForecolorProperty);            this.forecolor = lcolor;            //表头背景颜色            Brush brush = (Brush)this.GetValue(SimpleDataGrID.headerBackgroundProperty);            this.headerBackground = brush;            //背景颜色            brush = (Brush)this.GetValue(SimpleDataGrID.BackgroundProperty);            this.backGround = brush;            //指定列背景颜色            brush = (Brush)this.GetValue(SimpleDataGrID.ColumnBackgroundProperty);            this.columnBackground = brush;            //制定的列            string group = (string)this.GetValue(SimpleDataGrID.ColumnGroupsProperty);            this.ColumnGroups = group;        }        private voID NewGrID_MouseleftbuttonUp(object sender,MousebuttonEventArgs e)        {                    }        private voID NewGrID_MouseRightbuttonUp(object sender,MousebuttonEventArgs e)        {        }    }}

到此即写完整个GrIDVIEw控件,经过测试,能实现各种样式,基本达到预期效果!

总结

以上是内存溢出为你收集整理的用Grid写DataGrid控件全部内容,希望文章能够帮你解决用Grid写DataGrid控件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1076835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存