This is a migrated thread and some comments may be shown as answers.

Use .png files as tree node Icons

3 Answers 125 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Joel Palmer
Top achievements
Rank 2
Joel Palmer asked on 31 Dec 2009, 05:39 PM
I have the following hierarchy structure

Item (object)
- List<Item>Children (property)
- ItemType (property)

ItemType (object)
- ImageSource Icon (property)

My Icons are stored as .png resource files.  So, I can't load them directly.  Instead, I need to convert them from a Bitmap over to a BitmapSource.  They can then be used as the Icon (ImageSource).

Question is, does anyone have a good way in managed code to do this conversion?  I currently do this using interop but I would think there is a better way.  My current conversion method:

        public static BitmapSource LoadBitmap(Bitmap source)  
        {  
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(  
                source.GetHbitmap(),  
                IntPtr.Zero, Int32Rect.Empty,  
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());  
        } 

3 Answers, 1 is accepted

Sort by
0
Accepted
Valentin.Stoychev
Telerik team
answered on 04 Jan 2010, 09:42 AM
Hi Joel Palmer,

You don't need to do a conversion. The ImageSource type can be used with images that are inside an assembly. Please check this blog post for more info about the format of the uri that is used:
http://blogs.telerik.com/valentinstoychev/posts/08-10-02/loading_images_in_silverlight2_applications.aspx

Let us know if you need more info.

Sincerely yours,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Joel Palmer
Top achievements
Rank 2
answered on 04 Jan 2010, 04:40 PM
Okay.  Then here is where I get stuck.

I update the ItemType.Icon to an Image instead of an image source:

        public S88 Name { get; set; }  
        public Image Icon { get; set; } 

I previously had the HierarchicalDataTemplate as follows:

    <UserControl.Resources> 
        <HierarchicalDataTemplate x:Key="CheckBoxItemTemplate"   
            DataType="{x:Type this:Model.Item}" 
            ItemsSource="{Binding Children, Mode=OneTime}">  
            <StackPanel Orientation="Horizontal">  
                <Image Source="{Binding Path=ItemType.Icon}" /> 
                <ContentPresenter   
                        Content="{Binding Name, Mode=OneTime}"   
                        Margin="2,0" 
                        /> 
            </StackPanel> 
        </HierarchicalDataTemplate> 
    </UserControl.Resources> 

So, now how do I set up the Template now that my Source is actually an Image instead of an image source?  As it stands, I currently get no image with my TreeViewItem
0
Joel Palmer
Top achievements
Rank 2
answered on 04 Jan 2010, 06:32 PM
I get to answer my own question.  I went another direction and used the BitmapImage instead of the Image:

        S88 _name;  
        public S88 Name   
        {  
            get { return _name; }  
            set  
            {  
                _name = value;  
                setIcon();  
            }  
        }  
 
        public BitmapImage Icon { get; set; } 

Using the Template above, I now get images.  Thanks for your help.
Tags
TreeView
Asked by
Joel Palmer
Top achievements
Rank 2
Answers by
Valentin.Stoychev
Telerik team
Joel Palmer
Top achievements
Rank 2
Share this question
or