为DataGrid创建自定义列控件(二)
        通过上篇文章的学习,相信大家都对简单的自定义列控件比较熟悉了,在本节中将创建较为复杂的列控件,现在让我们开始吧。
相信大家肯定看到过这种情况,在评论的时候一些恶意的词语都会被**屏蔽掉,本节我们就来创建一个这样的审查的列控件CensorColumn。
在上篇文章的LimitColumn中,通过继承BoundColumn类,然后重写FormatDataValue方法,以达到格式字段内容的功能,本节的这个审查控件也可以通过这样的方法来做,如果大家感兴趣可以自己试试。在这里我们将采用另一种方法来作,通过继承DataGridColumn类来创建。
大家都知道BoundColumn类是继承自DataGridColumn类的,而BoundColumn类提供了DataField属性来显示数据源的字段,这次的CensorColumn类既然继承自DataGridColumn类,那我们也要创建DataField的这样一个属性来显示字段内容。
代码如下:![]() private string m_DataFiled;
private string m_DataFiled;
![]() public string DataField
        public string DataField
![]() {
        {
![]() get
            get
![]() {
            {
![]() return this.m_DataFiled;
                return this.m_DataFiled;
![]() }
            }
![]() set
            set
![]() {
            {
![]() this.m_DataFiled = value;
                this.m_DataFiled = value;
![]() }
            }
![]() }
        }
![]() 
        
光有属性还不够,无法把读取的数据源的字段显示出来,所以我们还需要把数据源的字段显示到Cell的Text上。这里我们就要用到TableCell的DataBinding事件。这个事件是当数据源绑定到控件上发生,就是说当数据源绑定到DataGrid上时激发TableCell的此事件。通过激发此事件,我们静态绑定数据源中指定的字段(通过刚才的DataField属性完成),就可以显示字段的内容了。
另外我们再来提下静态绑定,大家肯定都熟悉以下的语句:
DataBinder.Eval(Container.DataItem, "FieldName")
它是通过访问DataGridItem's DataItem属性来获取字段的内容,那我们怎样在自定义的事件方法中获得呢?首先我们从数据源中的单元格里获得DataGridItem:DataGridItem gridItem = cell.NamingContainer,
然后通过获得的DataGridItem获得DataItem:Object dataItem = gridItem.DataItem;这样我们就可以静态绑定数据了,代码如下:
![]() public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
![]() {
        {
![]() base.InitializeCell (cell, columnIndex, itemType);
            base.InitializeCell (cell, columnIndex, itemType);
![]() if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item))
            if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item))
![]() {
            {
![]() cell.DataBinding += new EventHandler(PerformDataBinding);
                cell.DataBinding += new EventHandler(PerformDataBinding);
![]() }
            }
![]() }
        }
![]()
![]() private void PerformDataBinding(object sender, System.EventArgs e)
        private void PerformDataBinding(object sender, System.EventArgs e)
![]() {
        {
![]() TableCell cell = (TableCell)sender;
            TableCell cell = (TableCell)sender;
![]() DataGridItem gridItem = (DataGridItem)cell.NamingContainer;
            DataGridItem gridItem = (DataGridItem)cell.NamingContainer;
![]() Object dataItem = gridItem.DataItem;
            Object dataItem = gridItem.DataItem;
![]()
![]() if(!this.m_DataFiled.Equals(string.Empty))
            if(!this.m_DataFiled.Equals(string.Empty))
![]() {
            {
![]() cell.Text = (string)DataBinder.Eval(dataItem, DataField);
                cell.Text = (string)DataBinder.Eval(dataItem, DataField);
![]() }
            }
![]() }
        }
![]()
接下来我们要做的就是根据要限制的字符来过滤内容了,代码如下:![]() //检查文本
//检查文本
![]() private string m_CensorText;
        private string m_CensorText;
![]() public string CensorText
        public string CensorText
![]() {
        {
![]() get
            get
![]() {
            {
![]() return this.m_CensorText;
                return this.m_CensorText;
![]() }
            }
![]() set
            set
![]() {
            {
![]() this.m_CensorText = value;
                this.m_CensorText = value;
![]() }
            }
![]() }
        }
![]()
![]() //替换文本
        //替换文本
![]() private string m_CensoredText = "***";
        private string m_CensoredText = "***";
![]() public string CensoredText
        public string CensoredText
![]() {
        {
![]() get
            get
![]() {
            {
![]() return this.m_CensoredText;
                return this.m_CensoredText;
![]() }
            }
![]() set
            set
![]() {
            {
![]() this.m_CensoredText = value;
                this.m_CensoredText = value;
![]() }
            }
![]() }
        }
![]() private string PerformShip(string text)
                private string PerformShip(string text)
![]() {
        {![]() if(m_CensorText.Equals(string.Empty))
            if(m_CensorText.Equals(string.Empty))
![]() {
            {
![]() return text;
                return text;
![]() }
            }
![]() else
            else 
![]() {
            {
![]() return text.Replace(this.m_CensorText,this.m_CensoredText);
                return text.Replace(this.m_CensorText,this.m_CensoredText);        
![]() }
            }    
![]() }
        }
![]()
最后的完成代码为:
![]() public class CensorColumn :DataGridColumn
    public class CensorColumn :DataGridColumn
![]() {
    {
![]() private string m_DataFiled;
        private string m_DataFiled;
![]() public string DataField
        public string DataField
![]() {
        {
![]() get
            get
![]() {
            {
![]() return this.m_DataFiled;
                return this.m_DataFiled;
![]() }
            }
![]() set
            set
![]() {
            {
![]() this.m_DataFiled = value;
                this.m_DataFiled = value;
![]() }
            }
![]() }
        }
![]()
![]() //检查文本
        //检查文本
![]() private string m_CensorText;
        private string m_CensorText;
![]() public string CensorText
        public string CensorText
![]() {
        {
![]() get
            get
![]() {
            {
![]() return this.m_CensorText;
                return this.m_CensorText;
![]() }
            }
![]() set
            set
![]() {
            {
![]() this.m_CensorText = value;
                this.m_CensorText = value;
![]() }
            }
![]() }
        }
![]()
![]() //替换文本
        //替换文本
![]() private string m_CensoredText = "***";
        private string m_CensoredText = "***";
![]() public string CensoredText
        public string CensoredText
![]() {
        {
![]() get
            get
![]() {
            {
![]() return this.m_CensoredText;
                return this.m_CensoredText;
![]() }
            }
![]() set
            set
![]() {
            {
![]() this.m_CensoredText = value;
                this.m_CensoredText = value;
![]() }
            }
![]() }
        }
![]()
![]()
![]() public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
        public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
![]() {
        {
![]() base.InitializeCell (cell, columnIndex, itemType);
            base.InitializeCell (cell, columnIndex, itemType);
![]() if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item))
            if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item))
![]() {
            {
![]() cell.DataBinding += new EventHandler(PerformDataBinding);
                cell.DataBinding += new EventHandler(PerformDataBinding);
![]() }
            }
![]() }
        }
![]()
![]() private void PerformDataBinding(object sender, System.EventArgs e)
        private void PerformDataBinding(object sender, System.EventArgs e)
![]() {
        {
![]() TableCell cell = (TableCell)sender;
            TableCell cell = (TableCell)sender;
![]() DataGridItem gridItem = (DataGridItem)cell.NamingContainer;
            DataGridItem gridItem = (DataGridItem)cell.NamingContainer;
![]() Object dataItem = gridItem.DataItem;
            Object dataItem = gridItem.DataItem;
![]()
![]() if(!this.m_DataFiled.Equals(string.Empty))
            if(!this.m_DataFiled.Equals(string.Empty))
![]() {
            {
![]() cell.Text = PerformShip((string)DataBinder.Eval(dataItem, DataField));
                cell.Text = PerformShip((string)DataBinder.Eval(dataItem, DataField));
![]() }
            }
![]() }
        }
![]()
![]() private string PerformShip(string text)
        private string PerformShip(string text)
![]() {
        {
![]() if(m_CensorText.Equals(string.Empty))
            if(m_CensorText.Equals(string.Empty))
![]() {
            {
![]() return text;
                return text;
![]() }
            }
![]() else
            else 
![]() {
            {
![]() return text.Replace(this.m_CensorText,this.m_CensoredText);
                return text.Replace(this.m_CensorText,this.m_CensoredText);        
![]() }
            }    
![]() }
        }
![]()
![]() }
    }
在DataGrid中加入列控件:
![]() <Columns>
<Columns>
![]() <custcols:CensorColumn CensorText="wit" DataField="ShipCountry"></custcols:CensorColumn>
                              <custcols:CensorColumn CensorText="wit" DataField="ShipCountry"></custcols:CensorColumn>
![]() </Columns>
                        </Columns>
![]()
效果图:![]()
通过本节大家对于自定义控件肯定有了更进一步的了解,在下一篇中我们将扩展本节的审查列控件,以达到多个单词的验证功能。
相信大家肯定看到过这种情况,在评论的时候一些恶意的词语都会被**屏蔽掉,本节我们就来创建一个这样的审查的列控件CensorColumn。
在上篇文章的LimitColumn中,通过继承BoundColumn类,然后重写FormatDataValue方法,以达到格式字段内容的功能,本节的这个审查控件也可以通过这样的方法来做,如果大家感兴趣可以自己试试。在这里我们将采用另一种方法来作,通过继承DataGridColumn类来创建。
大家都知道BoundColumn类是继承自DataGridColumn类的,而BoundColumn类提供了DataField属性来显示数据源的字段,这次的CensorColumn类既然继承自DataGridColumn类,那我们也要创建DataField的这样一个属性来显示字段内容。
代码如下:
 private string m_DataFiled;
private string m_DataFiled; public string DataField
        public string DataField {
        { get
            get {
            { return this.m_DataFiled;
                return this.m_DataFiled; }
            } set
            set {
            { this.m_DataFiled = value;
                this.m_DataFiled = value; }
            } }
        }
光有属性还不够,无法把读取的数据源的字段显示出来,所以我们还需要把数据源的字段显示到Cell的Text上。这里我们就要用到TableCell的DataBinding事件。这个事件是当数据源绑定到控件上发生,就是说当数据源绑定到DataGrid上时激发TableCell的此事件。通过激发此事件,我们静态绑定数据源中指定的字段(通过刚才的DataField属性完成),就可以显示字段的内容了。
另外我们再来提下静态绑定,大家肯定都熟悉以下的语句:
DataBinder.Eval(Container.DataItem, "FieldName")
它是通过访问DataGridItem's DataItem属性来获取字段的内容,那我们怎样在自定义的事件方法中获得呢?首先我们从数据源中的单元格里获得DataGridItem:DataGridItem gridItem = cell.NamingContainer,
然后通过获得的DataGridItem获得DataItem:Object dataItem = gridItem.DataItem;这样我们就可以静态绑定数据了,代码如下:
 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType) {
        { base.InitializeCell (cell, columnIndex, itemType);
            base.InitializeCell (cell, columnIndex, itemType); if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item))
            if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item)) {
            { cell.DataBinding += new EventHandler(PerformDataBinding);
                cell.DataBinding += new EventHandler(PerformDataBinding); }
            } }
        }
 private void PerformDataBinding(object sender, System.EventArgs e)
        private void PerformDataBinding(object sender, System.EventArgs e) {
        { TableCell cell = (TableCell)sender;
            TableCell cell = (TableCell)sender; DataGridItem gridItem = (DataGridItem)cell.NamingContainer;
            DataGridItem gridItem = (DataGridItem)cell.NamingContainer; Object dataItem = gridItem.DataItem;
            Object dataItem = gridItem.DataItem;
 if(!this.m_DataFiled.Equals(string.Empty))
            if(!this.m_DataFiled.Equals(string.Empty)) {
            { cell.Text = (string)DataBinder.Eval(dataItem, DataField);
                cell.Text = (string)DataBinder.Eval(dataItem, DataField); }
            } }
        }
接下来我们要做的就是根据要限制的字符来过滤内容了,代码如下:
 //检查文本
//检查文本 private string m_CensorText;
        private string m_CensorText; public string CensorText
        public string CensorText {
        { get
            get {
            { return this.m_CensorText;
                return this.m_CensorText; }
            } set
            set {
            { this.m_CensorText = value;
                this.m_CensorText = value; }
            } }
        }
 //替换文本
        //替换文本 private string m_CensoredText = "***";
        private string m_CensoredText = "***"; public string CensoredText
        public string CensoredText {
        { get
            get {
            { return this.m_CensoredText;
                return this.m_CensoredText; }
            } set
            set {
            { this.m_CensoredText = value;
                this.m_CensoredText = value; }
            } }
        } private string PerformShip(string text)
                private string PerformShip(string text) {
        { if(m_CensorText.Equals(string.Empty))
            if(m_CensorText.Equals(string.Empty)) {
            { return text;
                return text; }
            } else
            else  {
            { return text.Replace(this.m_CensorText,this.m_CensoredText);
                return text.Replace(this.m_CensorText,this.m_CensoredText);         }
            }     }
        }
最后的完成代码为:
 public class CensorColumn :DataGridColumn
    public class CensorColumn :DataGridColumn {
    { private string m_DataFiled;
        private string m_DataFiled; public string DataField
        public string DataField {
        { get
            get {
            { return this.m_DataFiled;
                return this.m_DataFiled; }
            } set
            set {
            { this.m_DataFiled = value;
                this.m_DataFiled = value; }
            } }
        }
 //检查文本
        //检查文本 private string m_CensorText;
        private string m_CensorText; public string CensorText
        public string CensorText {
        { get
            get {
            { return this.m_CensorText;
                return this.m_CensorText; }
            } set
            set {
            { this.m_CensorText = value;
                this.m_CensorText = value; }
            } }
        }
 //替换文本
        //替换文本 private string m_CensoredText = "***";
        private string m_CensoredText = "***"; public string CensoredText
        public string CensoredText {
        { get
            get {
            { return this.m_CensoredText;
                return this.m_CensoredText; }
            } set
            set {
            { this.m_CensoredText = value;
                this.m_CensoredText = value; }
            } }
        }

 public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
        public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType) {
        { base.InitializeCell (cell, columnIndex, itemType);
            base.InitializeCell (cell, columnIndex, itemType); if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item))
            if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item)) {
            { cell.DataBinding += new EventHandler(PerformDataBinding);
                cell.DataBinding += new EventHandler(PerformDataBinding); }
            } }
        }
 private void PerformDataBinding(object sender, System.EventArgs e)
        private void PerformDataBinding(object sender, System.EventArgs e) {
        { TableCell cell = (TableCell)sender;
            TableCell cell = (TableCell)sender; DataGridItem gridItem = (DataGridItem)cell.NamingContainer;
            DataGridItem gridItem = (DataGridItem)cell.NamingContainer; Object dataItem = gridItem.DataItem;
            Object dataItem = gridItem.DataItem;
 if(!this.m_DataFiled.Equals(string.Empty))
            if(!this.m_DataFiled.Equals(string.Empty)) {
            { cell.Text = PerformShip((string)DataBinder.Eval(dataItem, DataField));
                cell.Text = PerformShip((string)DataBinder.Eval(dataItem, DataField)); }
            } }
        }
 private string PerformShip(string text)
        private string PerformShip(string text) {
        { if(m_CensorText.Equals(string.Empty))
            if(m_CensorText.Equals(string.Empty)) {
            { return text;
                return text; }
            } else
            else  {
            { return text.Replace(this.m_CensorText,this.m_CensoredText);
                return text.Replace(this.m_CensorText,this.m_CensoredText);         }
            }     }
        }
 }
    }在DataGrid中加入列控件:
 <Columns>
<Columns> <custcols:CensorColumn CensorText="wit" DataField="ShipCountry"></custcols:CensorColumn>
                              <custcols:CensorColumn CensorText="wit" DataField="ShipCountry"></custcols:CensorColumn> </Columns>
                        </Columns>
效果图:
通过本节大家对于自定义控件肯定有了更进一步的了解,在下一篇中我们将扩展本节的审查列控件,以达到多个单词的验证功能。
 
                    
                     
                    
                 
                    
                
 
 
        
 
             
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号