浅谈DevExpress<五>:TreeList简单的美化——自定义单元格,加注释以及行序号

  今天就以昨天的列表为例,实现以下效果:预算大于110万的单元格突出显示,加上行序号以及注释,如下图:

  添加行序号要用到CustomDrawNodeIndicator方法,要注意的是,取得的节点索引是从0开始的,所以要+1以便第一行从一开始算起。

     private void treeList1_CustomDrawNodeIndicator(object sender, CustomDrawNodeIndicatorEventArgs e)

        {

            TreeList tree = sender as DevExpress.XtraTreeList.TreeList;

            tree.IndicatorWidth = 30;

            IndicatorObjectInfoArgs args = e.ObjectArgs as IndicatorObjectInfoArgs;

            args.DisplayText = (tree.GetVisibleIndexByNode(e.Node) + 1).ToString();

        }

  若要添加单元格注释,则要用到toolTipController控件:。将其拉入界面中,并将Treelist的tooltipcontroller属性设置为刚才的控件名称,然后定义控件的GetActiveObjectInfo事件,本例显示了单元格的内容、列和节点的索引:

        private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)

        {

            if (e.SelectedControl is DevExpress.XtraTreeList.TreeList)

            {

                TreeList tree = (TreeList)e.SelectedControl;

                TreeListHitInfo hit = tree.CalcHitInfo(e.ControlMousePosition);



                if (hit.HitInfoType == HitInfoType.Cell)

                {

                    object cellInfo = new TreeListCellToolTipInfo(hit.Node,hit.Column,null);

                    string toolTip = string.Format("{0} (Colomn: {1}, Node ID: {2})", hit.Node[hit.Column],

                        hit.Column.VisibleIndex, hit.Node.Id);

                    e.Info = new DevExpress.Utils.ToolTipControlInfo(cellInfo,toolTip);

                }

            }

        }

  最后,说一下自定义单元格,就是把符合条件的单元格按照定义的方式进行显示,例如本例是将预算大于110万的单元格背景变成粉色并且字体白色加粗显示。

        private void treeList1_NodeCellStyle(object sender, GetCustomNodeCellStyleEventArgs e)

        {

            if (e.Column.FieldName != "Budget") return;

            if (Convert.ToInt32(e.Node.GetValue(e.Column.AbsoluteIndex)) > 1100000)

            {

                e.Appearance.BackColor = Color.FromArgb(80,255,0,255);

                e.Appearance.ForeColor = Color.White;

                e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);

            }

        }

 

 

 

 

 

你可能感兴趣的:(DevExpress)