http://www.cnblogs.com/feilong3540717/articles/1980163.html
在最近的工作中,使用到了DevExpress中的XtraTreeList用法,正好借此机会研究一下此控件的应用,现将其应用技巧与大家分享一下。
1、使用XtraTreeList绑定数据(数据格式可以为ArrayList形式)
我们实例说明要实现下面样式的窗体:
首先,在Run Designer里面添加两个列(columns),设置两列的属性,其中第一列“名称”为string格式,FieldName为Name;第二列“选择”将ColumnEdit属性设置为CheckEdit1,FieldName为IsChecked,设置完毕。
其次可以编写一个类,用来绑定TreeList,针对此TreeList窗体,其类代码设置如下:
public class TestTreeList
{
publicTestTreeList()
{
}
//名称字段变量
privatestring m_sName = string.Empty;
//选择字段变量
privatebool m_bIsChecked = false;
//子Node节点ID变量
privateint m_iID = -1;
//父Node节点ID变量
privateint m_iParentID = -1;
publicint ID
{
get
{
return m_iID;
}
set
{
m_iID = value;
}
}
publicint ParentID
{
get
{
return m_iParentID;
}
set
{
m_iParentID = value;
}
}
publicstring Name
{
get
{
return m_sName;
}
set
{
m_sName = value;
}
}
publicbool IsChecked
{
get
{
return m_bIsChecked;
}
set
{
m_bIsChecked = value;
}
}
}
类构造完成,在FormLoad里面借助此类对TreeList进行绑定:
privatevoid Form1_Load(objectsender, EventArgs e)
{
ArrayListpList = new ArrayList();
TestTreeListp = new TestTreeList();
p.Name = "测试1";
p.IsChecked = true;
p.ID = 1;
pList.Add(p);
TestTreeListq = new TestTreeList();
q.Name = "测试2";
q.IsChecked = false;
q.ParentID = 1;
q.ID = 2;
pList.Add(q);
this.treeList1.DataSource= pList;
this.treeList1.RefreshDataSource();
}
这样就可以构造出一个简单的TreeList应用方法,程序运行结果可得第一幅图所示。
2、DevTreeList改变样式:
如果将TreeList控件改变成如下样式(同第一幅图不一样,将Check控件显示在节点最前面)。
可以直接修改其控件的ShowCheckBoxes属性即可(其它样式类似如下)。
3、DevTreeList非绑定形式实现:动态加载,构建:
运行样式如下:
实现动态加载的几个函数实现如下:
/// <summary>
/// 动态创建TreeList的Column,一列为图层名,一列为是否统计列Check控件
/// </summary>
privatevoid creatTreeListControl()
{
DevExpress.XtraTreeList.Columns.TreeListColumntreeListColumnLayerName= newDevExpress.XtraTreeList.Columns.TreeListColumn();
treeListColumnLayerName.Caption= "名称";
treeListColumnLayerName.FieldName = "LayerName";
treeListColumnLayerName.MinWidth = 38;
treeListColumnLayerName.Name = "treeListColumnLayerName";
treeListColumnLayerName.Visible = true;
treeListColumnLayerName.VisibleIndex = 0;
treeListColumnLayerName.Width = 111;
DevExpress.XtraTreeList.Columns.TreeListColumntreeListColumnStatisticValue=newDevExpress.XtraTreeList.Columns.TreeListColumn();
treeListColumnStatisticValue.Caption = "统计值";
treeListColumnStatisticValue.ColumnEdit = this.repositoryItemCheckEdit1;
treeListColumnStatisticValue.FieldName = "StatisticValue";
treeListColumnStatisticValue.Name = "treeListColumnStatisticValue";
treeListColumnStatisticValue.Visible= true;
treeListColumnStatisticValue.VisibleIndex = 1;
this.treeListLayerControl.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] {
treeListColumnLayerName,
treeListColumnStatisticValue});
this.treeListLayerControl.Nodes.Clear();
this.treeListLayerControl.Refresh();
}
/// <summary>
/// 动态创建TreeList Layer Node节点
/// </summary>
/// <paramname="layerName"></param>
/// <paramname="check"></param>
privatevoid creatMainLayerNode(string layerName,bool check)
{
this.treeListLayerControl.BeginUnboundLoad();
this.treeListLayerControl.AppendNode(new object[] { layerName,check}, -1);
this.treeListLayerControl.EndUnboundLoad();
}
/// <summary>
/// 动态创建TreeList 字段属性值Node节点
/// </summary>
/// <paramname="ValueName"></param>
/// <paramname="check"></param>
/// <paramname="ID"></param>
privatevoid CreatChildNode(stringValueName, bool check,intID)
{
this.treeListLayerControl.BeginUnboundLoad();
this.treeListLayerControl.AppendNode(new object[] {ValueName, check }, ID);
this.treeListLayerControl.EndUnboundLoad();
}
/// <summary>
/// 获得图层名节点的Index值
/// </summary>
/// <paramname="ParentNodeName"></param>
/// <returns></returns>
privateint getParentID(stringParentNodeName)
{
inti = -1;
for(i = 0; i < this.treeListLayerControl.Nodes.Count;i++)
{
if (this.treeListLayerControl.Nodes[i][0].ToString()== ParentNodeName)
{
break;
}
}
returni;
}
/// <summary>
/// 当点击Node事件发生改变(点击主节点时,其子节点跟着主节点变化)
/// </summary>
/// <paramname="sender"></param>
/// <paramname="e"></param>
privatevoid treeListLayerControl_CellValueChanging(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
{
if(e.Column.Name.Equals("treeListColumnStatisticValue")&& e.Node.HasChildren)
{
object objCol = this.treeListLayerControl.Columns[1];
for (int i = 0; i< e.Node.Nodes.Count; i++)
{
e.Node.Nodes[i].SetValue(objCol,e.Value);
}
e.Node.SetValue(objCol, e.Value);
}
}
创建子节点方法:
intindexLayerTree = this.getParentID(playerInfo.LayerName);
if (indexLayerTree == -1)
return;
CreatChildNode(sValue, false, this.treeListLayerControl.Nodes[indexLayerTree].Id);
其创建主节点和创建子节点方法类似,比创建子节点简单的多。
4、总结
以上为个人研究DevExpress TreeList使用的大致方法,尽管文笔简单潦草,但是针对TreeList的基本使用足矣。
另外,DevExpress控件可以使系统界面做的十分漂亮,我的截图界面显得丑陋,只因为是实验程序,并不是真正应用,还请大家不要误解。