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

How to Add Image Programmatically to RadTreeViewItem in WPF

9 Answers 355 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
EMRE ANDIC
Top achievements
Rank 1
EMRE ANDIC asked on 28 Apr 2010, 07:47 AM
Hi,
I want to  add image at run-time to a RadTreeViewItem in WPF.

9 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 30 Apr 2010, 04:50 PM
Hi EMRE ANDIC,

There are manu ways to acomplish this. However, since I am note reallt sure what are the requirement sof your scenario, I will suggest a cauple of approaches.

First, if you have a RadTreeView that is databound to a business object, you can add a property of type Uri to that business object. Then in the ItemTemplate for the RadTreeViewItems you can add an Image with Source databound to the value of the Uri property of your object.

Another approach is to use the DefaultImageSrc, ExpandedImageSrc and SelectedImageSrc properties of the RadTreeViewItem and set them in code behind:
item.DefaultImageSrc = "images/RedIcon.png";
Please keep in mind that in order to use the above properties you also need to set the ImagesBaseDir property of the RadTreeView. You can find more information about adding Item images here.

Take a look at the project and let me know if this is what you had in mind.


Sincerely yours,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Tina Stancheva
Telerik team
answered on 30 Apr 2010, 04:51 PM
Hi EMRE ANDIC,

There are many ways to accomplish this. However, since I am note really sure what are the requirements of your scenario, I will suggest a couple of approaches.

First, if you have a RadTreeView that is databound to a business object, you can add a property of type Uri to that business object. Then in the ItemTemplate for the RadTreeViewItems you can add an Image with Source databound to the value of the Uri property of your object.

Another approach is to use the DefaultImageSrc, ExpandedImageSrc and SelectedImageSrc properties of the RadTreeViewItem and set them in code behind:
item.DefaultImageSrc = "images/RedIcon.png";
Please keep in mind that in order to use the above properties, you also need to set the ImagesBaseDir property of the RadTreeView. You can find more information about adding Item images here.

Take a look at the project and let me know if this is what you had in mind.

Sincerely yours,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
EMRE ANDIC
Top achievements
Rank 1
answered on 03 May 2010, 02:10 PM
Hi,
I have a folder named images and I assign my image path in code-behind side

myRadTreeViewItem.DefaultImageSrc =

"images/add.png";

 

myRadTreeViewItem.ExpandedImageSrc =

"images/edit.png";

 

myRadTreeViewItem.SelectedImageSrc =

"images/editpaste.png";

and at Xaml side I assign also RadTreeView control's ImagesBaseDir property to  
ImagesBaseDir="/images/";

but still it is not working.

 

 
0
Tina Stancheva
Telerik team
answered on 03 May 2010, 02:21 PM
Hello EMRE ANDIC,

When you store all your images for the TreeView into a single folder you can use the ImagesBaseDir property of the RadTreeView to set the location of this folder. Then when settings the DefaultImagesSrc, ExpandedImageSrc, SelectedImageSrc properties of the RadTreeViewItem you will need to set only the image name, not the entire path to the image.

Therefore, in your case you can either set the ImagesBaseDir to "/" and keep the values of the DefaultImagesSrc, ExpandedImageSrc, SelectedImageSrc properties or you can set the DefaultImagesSrc, ExpandedImageSrc, SelectedImageSrc to: "add.png", "edit.png", "editpaste.png" accordingly and set the ImagesBaseDir to ImagesBaseDir="/images/", like you do.

Give it a try and let me know if it works for you.

Greetings,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
EMRE ANDIC
Top achievements
Rank 1
answered on 03 May 2010, 02:48 PM

Actually, I add my images to RadTreeViewItem at runtime, could it be a reason for the problem? Cause it is still the same. At Xaml side RadTreeView property still like that -> ImagesBaseDir="/images/"

 

 

 

 



RadTreeViewItem
rti;

 

 

for (int i = 0; i < ParentCount; i++)

 

{

rti =

new RadTreeViewItem();

 

rti.Header = "header";

 

for (int j = 0; j < ChildCount; j++)

 

{

rti.Items.Add("ChildName");

 

 

}

rti.DefaultImageSrc =

"add.png";

 

rti.ExpandedImageSrc =

"edit.png";

 

rti.SelectedImageSrc =

"editpaste.png";

 

 

myRadTreeView.Items.Add(rti);

}

0
Valentin.Stoychev
Telerik team
answered on 06 May 2010, 07:15 AM
Hello EMRE ANDIC,

the best will be if you send us your project so we can take a look at it. Thanks!

All the best,
Valentin.Stoychev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
EMRE ANDIC
Top achievements
Rank 1
answered on 06 May 2010, 03:01 PM
Hi Valentin.Stoychev,
I had been written RadTreeView property with a wrong assignment -> IsVirtualizing = True,  I changed its property to False, and now I can assign images to my RadTreeViewItems.

Thank you for your interest.

Best Regards,
Emre Andıç.
 
0
Tina Stancheva
Telerik team
answered on 07 May 2010, 12:52 PM
Hello EMRE ANDIC,

We are glad that you got it working.

Let us know if we can further assist you.

Best wishes,
Tina Stancheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
EMRE ANDIC
Top achievements
Rank 1
answered on 10 May 2010, 07:42 AM
Hello Tina Stancheva,

thank you so much.

Best Regards,
Emre Andıç.
Tags
TreeView
Asked by
EMRE ANDIC
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
EMRE ANDIC
Top achievements
Rank 1
Valentin.Stoychev
Telerik team
Share this question
or