我目前正在使用mono和gtk#。 关于TreeView,有Tutorial。 我想实现与“控制模型的使用方式”相同的内容 所以我有Song Class和Render-Methods来显示艺术家和标题。
但是我希望通过TreeStore而不是ListStore来显示它。因此,我为每个字母都有一个Rootnode,在此节点下,所有以此字母开头的艺术家都应该显示。
我的问题如何将这些RootNode添加到TreeStore?我应该在哪里添加它们?
songs.Add(new Song("Dancing Djs vs. Roxette", "Fading like a flower"));
songs.Add(new Song("Xaiver","give me the right"));
songs.Add(new Song("Daft Punkt","Technologic"));
TreeStore musicListStore = new TreeStore(typeof(Song));
foreach (var s in songs)
{
musicListStore.AppendValues(s);
}
treeview1.Model = musicListStore;
treeview1.AppendColumn("Artist", new CellRendererText(),new TreeCellDataFunc(RenderArtistName));
treeview1.AppendColumn("Title", new CellRendererText(),new TreeCellDataFunc(RenderSongTitle));
private void RenderArtistName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
{
Song s = model.GetValue(iter,0) as Song;
(cell as CellRendererText).Text = s.Artist;
}
private void RenderSongTitle(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
{
Song s = model.GetValue(iter,0) as Song;
(cell as CellRendererText).Text = s.Title;
}
所以我想要实现每个字母都有RootNodes,每个字母下面都应该有从这封信开始列出的所有艺术家。
我的问题是如何将字母添加到TreeStore以及我如何知道在哪里插入每首歌曲。
以下是我希望它看起来的截图(我不允许直接上传它们。所以我必须使用外部托管.Sry): Screenshot
答案 0 :(得分:2)
您可以非常轻松地构建树。例如;
var store = new Gtk.TreeStore( typeof(string) );
// add a root node
var root = store.AppendValues("hello");
// add a child of the root
store.AppendValues(root,"world");
// add another child
var mono = store.AppendValues(root,"mono");
// add a second level child
store.AppendValues(mono,"gtk");
所以,在你的音乐应用的背景下..
// title, artist
var store = new Gtk.TreeStore( typeof(string), typeof(string) );
// make an index of top level nodes using thier TreeIters
var index = new Dictionary<string,Gtk.TreeIter>();
// add index nodes
foreach ( var letter in new List<string>{ "A", "B", "C" ... "Z" } ){
index[letter.ToLower()] = store.AppendValues( letter );
}
// add songs
foreach ( var song in songlist ){
var title = song.Title;
var artist = song.Artist;
var first = title.SubString(0,1).ToLower();
var iter = store[first];
// add this song
store.AppendValues( iter, title, artist );
}
如果要动态添加索引节点,则必须执行一些额外的工作,每次在某个级别添加节点时,该级别或更深层次的所有树特值都将变得毫无价值。
答案 1 :(得分:0)
songs.Add(new Song("Dancing Djs vs. Roxette", "Fading like a flower"));
songs.Add(new Song("Xaiver","give me the right"));
songs.Add(new Song("Daft Punkt","Technologic"));
TreeStore musicListStore = new TreeStore(typeof(Song));
foreach (var s in songs)
{
musicListStore.AppendValues(s);
}
treeview1.Model = musicListStore;
treeview1.AppendColumn("Artist", new CellRendererText(),new TreeCellDataFunc(RenderArtistName));
treeview1.AppendColumn("Title", new CellRendererText(),new TreeCellDataFunc(RenderSongTitle));
private void RenderArtistName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
{
Song s = model.GetValue(iter,0) as Song;
(cell as CellRendererText).Text = s.Artist;
}
private void RenderSongTitle(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
{
Song s = model.GetValue(iter,0) as Song;
(cell as CellRendererText).Text = s.Title;
}
所以我想要实现每个字母都有RootNodes,每个字母下面都应该有从这封信开始列出的所有艺术家。
我的问题是如何将字母添加到TreeStore以及我如何知道在哪里插入每首歌曲。
以下是我希望它看起来的截图(我不允许直接上传它们。所以我必须使用外部托管.Sry): Screenshot