我正在尝试创建一个显示类似信息的treelistview
ComputerName(String)
-> OS(String)
-> Drives(String)
-> DriveLabel(String)
我不知道为什么但是我无法根据我的对象模型创建树视图,可能会发生什么?我现在已经搞砸了一段时间了,也许我只是混淆了我的自我,但不应该这么难用这个控制......任何帮助都是值得赞赏的。谢谢!
这是我的班级
public class ComputerInfo
{
public string ComputerName { get; set; }
public string OS { get; set; }
public string Pingable { get; set; }
public List<string> Memory { get; set; }
public List<string> Drives { get; set; }
public ComputerInfo()
{
this.Memory = new List<string>();
this.Drives = new List<string>();
}
}
根据我的阅读,我需要在加载期间放置代理代码
public Form1()
{
InitializeComponent();
this.treeListView1.CanExpandGetter = delegate(object x)
{
return true;
};
this.treeListView1.ChildrenGetter = delegate(object x)
{
NetworkDefrag.DefragClass.ComputerInfo pc = x as NetworkDefrag.DefragClass.ComputerInfo;
return pc.Drives;
};
}
这是我的实际工作代码
public void work2(string line)
{
NetworkDefrag.DefragClass.ComputerInfo computer = new DefragClass.ComputerInfo();
computer.ComputerName = line;
//1
bool pingable = false;
Task pingTask = new Task(() => pingable = PingHost(line));
pingTask.Start();
pingTask.Wait();
//2
if (pingable.Equals(true))
{
TreeNode servernameNode = new TreeNode(line);
computer.Pingable = "Server is online";
//3
//var drives = GetDrives(line, domaintextBox3.Text, usernametextBox1.Text, passwordtextBox2.Text);
List<string> drives = new List<string>();
Task drivesTask = new Task(() => drives = GetDrives(line, domaintextBox3.Text, usernametextBox1.Text, passwordtextBox2.Text));
computer.Drives.AddRange(drives);
drivesTask.Start();
drivesTask.Wait();
//4
foreach (string drive in drives)
{
//TreeNode driveNode = new TreeNode(drive);
//5
string fragNeeded = string.Empty;
//var fragNeeded = GetFragPercentage(line, drive, usernametextBox1.Text, passwordtextBox2.Text);
Task fragNeededTask = new Task(() => fragNeeded = GetFragPercentage(line, drive, usernametextBox1.Text, passwordtextBox2.Text));
fragNeededTask.Start();
fragNeededTask.Wait();
//var fragPercentage = GetFragPercentage(line, drive, usernametextBox1.Text, passwordtextBox2.Text);
if (fragNeeded.Equals("True"))
{
TreeNode driveNode = new TreeNode(drive);
driveNode.BackColor = Color.Red;
driveNode.Checked = true;
servernameNode.Checked = true;
InsertIntoTree2(servernameNode, driveNode);
}
if (fragNeeded.Equals("False"))
{
TreeNode driveNode = new TreeNode(drive);
driveNode.BackColor = Color.Green;
InsertIntoTree2(servernameNode, driveNode);
}
}
Servers.Add(computer);
InsertIntoTree(servernameNode);
//treeListView1.UpdateObjects(Servers);
treeListView1.RefreshObject(computer);
}
}
更新:
我没有在列编辑器中分配Aspect值,但现在我已经修复了,节点现在有了文本!我的问题实际上是子节点(驱动节点),那些没有按预期显示(可扩展)