.net - Add icon to WPF TreeViewItem at runtime -


there many samples demonstrating in xaml, such following:

<treeviewitem> <treeviewitem.header> <stackpanel orientation="horizontal"> <image source="..."/> <textblock>hello</textblock> </stackpanel> </treeviewitem.header>  </treeviewitem> 

but need in runtime code - purpose of treeview show files , folders on computer.

so i'm not sure how work header in code:

 each f in directory.getfiles()          dim icon = system.drawing.icon.extractassociatedicon(f.fullname)         dim name string = f.name              dim item new treeviewitem          item.header = ...    next 

can demonstrate concept please?

edit: think i'm getting it, should use horizontal stackpanel 2 separate controls - textblock , image. right approach?

.

here sample code how should start. understand first, , make appropriate change meet need. code written in c# , xaml. hope you'll understand c# , able convert basic.

    public class nameiconpair     {         public string name { get; set; }         public bitmapsource iconsource { get; set; }     }      public partial class window1 : window     {         public window1()         {             initializecomponent();              var files = system.io.directory.getfiles("e:\\");             observablecollection<nameiconpair> pairs = new observablecollection<nameiconpair>();             foreach (string file in files)             {                 system.drawing.icon icon = system.drawing.icon.extractassociatedicon(file);                 stream stream = new memorystream();                 icon.save(stream);                 bitmapdecoder decoder = iconbitmapdecoder.create(stream, bitmapcreateoptions.none, bitmapcacheoption.none);                 bitmapsource src = decoder.frames[0];                 pairs.add(new nameiconpair() { name = file,  iconsource = src });             }             this.datacontext = pairs;         }     } 

and here xaml code:

    <treeview itemssource="{binding}">         <treeview.itemtemplate>             <datatemplate>                 <stackpanel orientation="horizontal">                     <image source="{binding iconsource}"/>                     <textblock text="{binding name}"/>                 </stackpanel>             </datatemplate>         </treeview.itemtemplate>     </treeview> 

hope, sample code greatly. :-)

.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -