我想要一个 NodeView 来显示一些分层数据,如下所示:
Father Mother
====== ======
Jon Ann
+Sons
+--Jon 20
+--Dave 10
+Daughters
+--Ann
Ron Mary
Paul Eve
+Sons
+--Bob 4
如果“儿子”和“女儿”为空,则不应显示它们。我创建了三个业务类:Parents、Son 和 Daughter,并且我正在创建 TreeNode 子类来显示它们。我用 Monodevelop 设计器设计了一个窗口,并用代码重新创建了 nodeView。nodeview根本不显示任何东西,我想知道为什么。这是代码,在一个文件中,所以任何人都可以测试它:
using System;
using Gtk;
using System.Collections.Generic;
using System.Linq;
namespace Family.Model
{
public class Son {
public string Name {get;set;}
public string Age {get;set;}
public Son(string n,string a) {
Name=n;Age=a;
}
}
public class Parents
{
public string Father {get;set;}
public string Mother {get;set;}
public List<string> Daughters {get;set;}
public Dictionary<string,Son> Sons {get;set;}
public Parents() {
Daughters=new List<string>();
Sons=new Dictionary<string, Son>();
}
}
}
namespace Family.View
{
using Family.Model;
[TreeNode (ListOnly=false)]
public class ParentsNode:TreeNode
{
private Parents parents;
public ParentsNode (Parents p):base()
{
this.parents=p;
DaughtersRoot rootd=new DaughtersRoot();
SonsRoot roots=new SonsRoot();
if (p.Sons.Count>0) {
this.AddChild (roots);
p.Sons.Values.ToList ().ForEach (x=>roots.AddChild(new SonNode(x)));
}
if (p.Daughters.Count>0) {
this.AddChild (rootd);
p.Daughters.ForEach (x=>rootd.AddChild(new DaughterNode(x)));
}
OnChanged ();
}
[Gtk.TreeNodeValue(Column=0) ]
public string Father {
get { return parents.Father;}
set { parents.Father=value;OnChanged ();}
}
[Gtk.TreeNodeValue (Column=1)]
public string Mother {
get { return parents.Mother; }
set { parents.Mother=value;OnChanged ();}
}
}
[TreeNode(ListOnly=false)]
public class DaughtersRoot:TreeNode
{
[Gtk.TreeNodeValue(Column=0) ]
public string Label {
get {return "Daughters"; }
}
}
[TreeNode(ListOnly=false)]
public class SonsRoot:TreeNode
{
[Gtk.TreeNodeValue(Column=0) ]
public string Label {
get {return "Sons"; }
}
}
[TreeNode(ListOnly=false)]
public class DaughterNode:TreeNode {
private string mName;
public DaughterNode(string s):base() {
this.Name=s;
}
[Gtk.TreeNodeValue(Column=0) ]
public string Name {
get {return mName;}
set {mName=value;OnChanged ();}
}
}
[TreeNode(ListOnly=false)]
public class SonNode:TreeNode {
private Son son;
public SonNode(Son s):base() {
this.son=s;
OnChanged ();
}
[Gtk.TreeNodeValue(Column=0)]
public string Name {
get { return this.son.Name; }
set {son.Name=value;OnChanged ();}
}
[Gtk.TreeNodeValue(Column=1)]
public string Age {
get { return this.son.Age; }
set {son.Age=value;OnChanged ();}
}
}
public class MainWindow: Gtk.Window
{
private global::Gtk.ScrolledWindow GtkScrolledWindow;
private global::Gtk.NodeView treeFamily;
private NodeStore storeParents=new NodeStore(typeof(ParentsNode));
protected virtual void Build ()
{
global::Stetic.Gui.Initialize (this);
// Widget MainWindow
this.Name = "MainWindow";
this.Title = global::Mono.Unix.Catalog.GetString ("MainWindow");
this.WindowPosition = ((global::Gtk.WindowPosition)(4));
this.GtkScrolledWindow = new global::Gtk.ScrolledWindow ();
this.GtkScrolledWindow.Name = "GtkScrolledWindow";
this.GtkScrolledWindow.ShadowType = ((global::Gtk.ShadowType)(1));
this.treeFamily = new global::Gtk.NodeView ();
this.treeFamily.CanFocus = true;
this.treeFamily.Name = "treeFamily";
this.GtkScrolledWindow.Add (this.treeFamily);
this.Add (this.GtkScrolledWindow);
if ((this.Child != null)) {
this.Child.ShowAll ();
}
this.DefaultWidth = 400;
this.DefaultHeight = 300;
this.Show ();
this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
}
public MainWindow (): base (Gtk.WindowType.Toplevel) {
Build ();
Parents p=new Parents();
p.Father="Bob";
p.Mother="Mary";
storeParents.AddNode (new ParentsNode (p));
p=new Parents();
p.Father="Ron";
p.Mother="Ann";
p.Sons.Add ("David",new Son("David","20"));
p.Sons.Add ("Matt",new Son("Matt","10"));
p.Daughters.Add ("Elaine");
p.Daughters.Add ("Kate");
storeParents.AddNode (new ParentsNode(p));
this.treeFamily=new NodeView(storeParents);
Gtk.TreeViewColumn fatherColumn = new Gtk.TreeViewColumn
("Father",new CellRendererText(),"text",0);
Gtk.TreeViewColumn motherColumn = new Gtk.TreeViewColumn
("Mother",new CellRendererText(),"text",1);
treeFamily.AppendColumn (fatherColumn);
treeFamily.AppendColumn (motherColumn);
treeFamily.ShowAll ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a) {
Application.Quit ();
a.RetVal = true;
}
}
class MainClass
{
public static void Main (string[] args) {
Application.Init ();
MainWindow win = new MainWindow ();
win.Show ();
Application.Run ();
}
}
}