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

Can a TreeView Item have its Image Source Set via C# code?

1 Answer 177 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Anthony Pieper
Top achievements
Rank 1
Anthony Pieper asked on 08 Jul 2009, 11:58 PM
We are using Visual Studio 2008 and have a Project File with a Media folder underneath that holds images of icons, also within the project is the page's xaml and xaml.cs files.

We want to set a treeview item image icon dynamically based on certain conditions instead of at designtime from certain icons in the Media folder.

The name of the icon in my Media folder mentioned above is MYIMAGE.PNG. I have the following C# code I was testing but it isn't working:

BitmapImage bitmap = new BitMapImage();
string imagePath = "../Media/MYIMAGE.PNG";
bitmap.UriSource = new Uri(imagePath, UriKind.Relative);
((RadTreeViewItem)this.trvBookContent.ItemContainerGenerator.ContainerFromItem(trvBookContent.Items[0])).DefaultImageSrc = Convert.ToString(bitmap);

I also noticed there is a SelectedImageSrc and ExpandedImageSrc property.

Any ideas on how to set an image dynamically for a treeview item?

Thanks

1 Answer, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 09 Jul 2009, 06:57 AM
Hello Anthony Pieper,

Yes - it is possible. The problem with your code is that the DefaultImageSrc needs to be pointed to the path of the image, not to the image content.

So in your case the code will look like:

string imagePath = "../Media/MYIMAGE.PNG";

((RadTreeViewItem)this.trvBookContent.ItemContainerGenerator.ContainerFromItem(trvBookContent.Items[0])).DefaultImageSrc = imagePath;

Please let us know if you face any other problems.

Greetings,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
TreeView
Asked by
Anthony Pieper
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Share this question
or