转:TreeList属性控件的控制
最近项目中要求用dev 控件的treelist 树形控件。
如下图
要求如下:
1:选择父节点后,子节点全部打钩;
2:选择子节点而不选择父节点,则从当前节点的父节点一直到根节点check框都是半选状态。
具体实现如下:
一、treelist绑定的数据源是数据集(dateset)中的表,而且必须给:(当前节点id)KeyFieldName和(上一级节点id)ParentFieldName 两个属性绑定字段。
二、设置treelist1(名称)中OptionView—ShowCheckBoxes属性为true,则每个节点前面自动增加一个check框。
三、设置OptionBehavior—AllIndeterminateCheckState属性为true,则check框可以出现半选状态。
四、
namespace MyProject
{
public partial class MainFrm : DockContent
{
tbEngProjectBll objtbEngProjectBll = new tbEngProjectBll();
tbFileClassBll _tbFileClassBll = new tbFileClassBll();
tbModelBll _tbModelBll = new tbModelBll();
public static MainFrm MainFrmform1;
private 模型.ThreeDModel BimModelForm { get; set; }
#region 构造函数
/// <summary>
///构造函数
/// </summary>
public MainFrm()
{
InitializeComponent();
MainFrmform1 = this;
this.tsUserName.Text = ProjectActive.LoginUserName != null ? ProjectActive.LoginUserName : string.Empty;
FromSet();
ToolSToolbarEnabledState(false);
this.panelDock.AllowEndUserDocking = false;
//在winform下使初始化速度慢的数据可以在窗体显示之后再加载,这样就能提高用户体验
new DelayLoading(this, 500, new MethodInvoker(Panel_Show)).Execute();
// Panel_Show();
SynergyModel.MassDockPanel = this.panelDock;
}
private void ToolSToolbarEnabledState(bool bol)
{
foreach (ToolStripItem item in this.toolSToolbar.Items)
{
switch (item.Text)
{
case "工程台账":
case "切换用户":
item.Enabled = true;
break;
default:
item.Enabled = bol;
break;
}
}
this.tsUserName.Enabled = true;
}
/// <summary>
/// 设置
/// </summary>
private void FromSet()
{
this.IsMdiContainer = true;
/*设置浮动停靠控件的样式*/
this.panelDock.Theme = new WeifenLuo.WinFormsUI.Docking.VS2015LightTheme();
//标题设置
TitleText();
//工具栏设置手柄可见性
toolSToolbar_GripStyle();
}
#endregion
#region 界面设置区域
#region 工具栏样式设置,按钮操作
/// <summary>
/// 工具栏设置手柄可见性
/// </summary>
private void toolSToolbar_GripStyle()
{
this.toolSToolbar.GripStyle = ToolStripGripStyle.Hidden;
}
#region 样式设置
/// <summary>
/// 菜单栏去白色边框
/// 问题:在C#中使用toolstrip时,当RenderMode设置为System时会出现下面有条灰线,无法通过基本的设置属性除去;
/// 解决办法:只需要重绘一下toolstrip即可
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolSToolbar_Paint(object sender, PaintEventArgs e)
{
if ((sender as ToolStrip).RenderMode == ToolStripRenderMode.System)
{
Rectangle rect = new Rectangle(0, 0, this.toolSToolbar.Width, this.toolSToolbar.Height - 2);
e.Graphics.SetClip(rect);
}
}
#endregion
#endregion
#region 标题设置
private void TitleText()
{
// this.lblTitle.Text = "晨曦CEBIM";
this.Text = "CEBIM";
}
#endregion
#region 标题创建
/// <summary>
/// 标题创建
/// </summary>
public void Title_Create(object sender)
{
string title = "CEBIM-" + ProjectActive.ProjectName;
//this.lblTitle.Text = title.Length > 20 ? string.Format(title.Substring(0, 20) + "...") : title;
//this.lblTitle.Text = title;
//this.lblTitle.TextAlign = ContentAlignment.MiddleCenter;
this.Text = title;
}
#endregion
#endregion
#region 功能实现区域
private void Panel_Show()
{
toolSMICut_Click(null, null);
}
/// <summary>
/// 设置第三方控件
/// </summary>
private void Panel_DockPanel()
{
if (!panelDock.Contents.Any(u => u.GetType() == typeof(模型.ThreeDModel)))
{
/*中间3维模型*/
BimModelForm = new 模型.ThreeDModel();
this.openOrActiveDockContent(BimModelForm, DockState.Document);
}
}
#endregion
#region 私有方法,打开或激活可停靠窗口
/// <summary>
/// 打开或激活可停靠窗口
/// </summary>
/// <typeparam name="T">DockContent窗口</typeparam>
private void openOrActiveDockContent<T>(DockState dockState = DockState.Document) where T : DockContent, new()
{
if (panelDock.Contents.Any(u => u.GetType() == typeof(T)))
{
(panelDock.Contents.FirstOrDefault(u => u.GetType() == typeof(T)) as DockContent).Activate();
}
else
{
DockContent dockContent = new T();
dockContent.Show(panelDock, dockState);
}
}
/// <summary>
/// 打开或激活可停靠窗口
/// </summary>
/// <param name="dockContent">DockContent窗口</param>
public void openOrActiveDockContent(DockContent dockContent, DockState dockState = DockState.Document)
{
/*需要判断是否重复打开窗口,如果已经打开,直接激活*/
if (panelDock.Contents.Any(u => u.GetType() == dockContent.GetType()))
{
(panelDock.Contents.FirstOrDefault(u => ((DockContent)u).TabText == dockContent.TabText) as DockContent).Activate();
dockContent.Dispose();
}
else
{
dockContent.Show(panelDock, dockState);
}
}
#endregion
/// <summary>
/// 查看模型列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsModelManage_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<模型.ModelGrid>(DockState.DockBottom);
}
/// <summary>
/// 查看工程概况
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsProjectProfile_Click(object sender, EventArgs e)
{
//this.openOrActiveDockContent<工程.ProjectInfoFrm>(DockState.DockBottom);
//ProjectInfoFrm objProjectInfoFrm = new ProjectInfoFrm();
//objProjectInfoFrm.updateProject += new ProjectInfoFrm.UpdateProject(Title_Create);
//objProjectInfoFrm.Show(panelDock, DockState.DockBottom);
/*初步测试中*/
ProjectProfileShow();
}
public void ProjectProfileShow()
{
ProjectMessage objProjectMessage = new ProjectMessage();
objProjectMessage.cellValueChanged += new ProjectMessage.CellValueChanged(Title_Create);
openOrActiveDockContent(objProjectMessage, DockState.DockBottom);
}
/// <summary>
/// 成本管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsAmountPrice_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<Cost.CostManageForm>(DockState.DockBottom);
}
private void tsWorksProgramme_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<进度.GanttChartFrm>(DockState.DockBottom);
}
private void tsQRCode_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<QRcode.QRcodeFrm>(DockState.DockBottom);
}
private void tsTask_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<Synergy.TaskForm>();
}
/// <summary>
/// 问题列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmQuestionList_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<Synergy.QuestionFrm>();
}
/// <summary>
/// 新建项目
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolSMINew_Click(object sender, EventArgs e)
{
CutProjectFrm objCutProjectFrm = new CutProjectFrm();
if (ProjectActive.ServerConnectionState)
{
objCutProjectFrm.CreateProject();
PanelClose(null);
}
}
/// <summary>
///切换项目
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolSMICut_Click(object sender, EventArgs e)
{
ToolSToolbarEnabledState(false);
FrmClose();
CutProjectFrm objCutProjectFrm = new CutProjectFrm();
objCutProjectFrm.refreshParent += new CutProjectFrm.RefreshParentForm(PanelClose);
objCutProjectFrm.TopLevel = false;
objCutProjectFrm.MdiParent = this;
objCutProjectFrm.StartPosition = FormStartPosition.CenterScreen;
objCutProjectFrm.Parent = this.panelDock;
// objCutProjectFrm.WindowState = FormWindowState.Maximized;
objCutProjectFrm.FormBorderStyle = FormBorderStyle.None;
objCutProjectFrm.Dock = DockStyle.Fill;/*这边需要设置,才能让嵌套窗体的listview里面显示滚动条出来*/
objCutProjectFrm.Show();
}
/// <summary>
/// 关闭DockPanle窗体
/// </summary>
/// <param name="sender"></param>
private void PanelClose(object sender)
{
SynergyModel.MaterialMontioring = null;
/*做窗口关闭*/
this.Invoke(new Action(() =>
{
/*解决重复打开项目时,内容持续增加的问题—— 将停靠区域的窗体释放内存并关闭 add by timfeng 2018-4-12 9:25:41*/
for (int i = panelDock.Contents.Count - 1; i >= 0; i--)
{
var item = panelDock.Contents.ElementAt(i) as DockContent;
item.Close();
item.Dispose();
}
}));
//在winform下使初始化速度慢的数据可以在窗体显示之后再加载,这样就能提高用户体验
CloseShowData();
}
private void CloseShowData()
{
Task.Run(() =>
{
Thread.Sleep(500);
this.Invoke(new Action(() =>
{
this.tsUserName.Text = ProjectActive.LoginUserName;
FromSet();
//标题创建
Title_Create(null);
ToolSToolbarEnabledState(true);
}));
Thread.Sleep(500);
/*加载模型列表*/
this.Invoke(new Action(() =>
{
this.openOrActiveDockContent<模型.ModelGrid>(DockState.DockBottom);
}));
Thread.Sleep(500);
/*加载选择集列表*/
this.Invoke(new Action(() =>
{
this.openOrActiveDockContent<模型.GenerateSelectSet>(DockState.DockLeft);
}));
Thread.Sleep(500);
/*加载模型主窗口*/
this.Invoke(new Action(() =>
{
Panel_DockPanel();
}));
Thread.Sleep(500);
this.Invoke(new Action(() =>
{
new MaterialMontioring().Material();/*材料跟踪*/
}));
});
}
private void tsQuestion_ButtonClick(object sender, EventArgs e)
{
this.openOrActiveDockContent<Synergy.QuestionFrm>();
}
private void tsDocument_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<资料管理.DocumentFrm>(DockState.DockBottom);
}
/// <summary>
/// CAD图纸管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsCADFile_Click(object sender, EventArgs e)
{
var form = new CADFile.CADFileForm(BimModelForm);
this.openOrActiveDockContent(form, DockState.DockBottom);
}
/// <summary>
///协同管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsSynergy_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<Synergy.ShowSynergyFrm>(DockState.DockBottom);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
ProjectActive.AutomaticLoginStatus = false;
LoginFrm log = new LoginFrm();
if (log.ShowDialog() == DialogResult.OK)
{
this.Controls.Clear();
InitializeComponent();
this.tsUserName.Text = ProjectActive.LoginUserName;
ProjectActive.ProjectName = string.Empty;
FromSet();
//标题创建
Title_Create(null);
this.toolSToolbar.Enabled = true;
toolSMICut_Click(null, null);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.cmsTool.Show(this.button1, 0, this.button1.Height);
}
private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是否退出?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
System.Windows.Forms.Application.ExitThread();
//System.Environment.Exit(0);
}
else
{
e.Cancel = true;
}
}
private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void FrmClose()
{
/*解决重复打开项目时,内容持续增加的问题—— 将停靠区域的窗体释放内存并关闭 add by timfeng 2018-4-12 9:25:41*/
for (int i = panelDock.Contents.Count - 1; i >= 0; i--)
{
var item = panelDock.Contents.ElementAt(i) as DockContent;
//if (item.GetType().Name == "ThreeDModel")
//{
item.Close();
item.Dispose();
// }
}
}
private void tsTZ_Click(object sender, EventArgs e)
{
toolSMICut_Click(null, null);
}
/// <summary>
/// 变更对比
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsCompare_Click(object sender, EventArgs e)
{
this.openOrActiveDockContent<模型.ModelDiffDetailForm>(DockState.DockBottom);
}
}
}
七、如果想 一个 node 绑定是 name 字段,但是显示为:code+name 可以在(CustomDrawNodeCell)这个事件中写方法
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->private void TreeCat_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
{
if (e.CellValue == null) return;
if (e.Node == null) return;
if (e.Column.Equals(name))
{
if (e.CellValue.ToString().Trim() != string.Empty)
{
System.Data.DataRowView rov = treeList1.GetDataRecordByNode(e.Node) as System.Data.DataRowView;
if (rov != null)
{
e.CellText = rov["code"].ToString().Trim() + " - " + rov["name"].ToString().Trim();
}
}
}
}
以上就是目前对treelist的一些基本操作。主要是用到递归方法实现对节点的控制。