0

次のような階層データを NodeView に表示させたいと思います。

Father Mother
====== ======
Jon    Ann
 +Sons
 +--Jon  20
 +--Dave 10
 +Daughters
 +--Ann  
Ron    Mary
Paul   Eve
 +Sons
 +--Bob  4

「Sons」と「Daughters」は、空の場合は表示されません。Parents、Son、Daughter という 3 つのビジネス クラスを作成し、それらを表示するために TreeNode サブクラスを作成しています。Monodevelop デザイナーで Window を設計し、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 ();
        }
    }
}
4

1 に答える 1