How to Add Image Programmatically to RadTreeViewItem in WPF

10 posts, 0 answers
  1. EMRE ANDIC
    EMRE ANDIC avatar
    13 posts
    Member since:
    Jan 2010

    Posted 28 Apr 2010 Link to this post

    Hi,
    I want to  add image at run-time to a RadTreeViewItem in WPF.
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Apr 2010 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Apr 2010 Link to this post

    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.
  5. EMRE ANDIC
    EMRE ANDIC avatar
    13 posts
    Member since:
    Jan 2010

    Posted 03 May 2010 Link to this post

    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.

     

     
  6. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 03 May 2010 Link to this post

    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.
  7. EMRE ANDIC
    EMRE ANDIC avatar
    13 posts
    Member since:
    Jan 2010

    Posted 03 May 2010 Link to this post

    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);

    }

  8. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 06 May 2010 Link to this post

    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.
  9. EMRE ANDIC
    EMRE ANDIC avatar
    13 posts
    Member since:
    Jan 2010

    Posted 06 May 2010 Link to this post

    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ıç.
     
  10. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 07 May 2010 Link to this post

    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.
  11. EMRE ANDIC
    EMRE ANDIC avatar
    13 posts
    Member since:
    Jan 2010

    Posted 10 May 2010 Link to this post

    Hello Tina Stancheva,

    thank you so much.

    Best Regards,
    Emre Andıç.
Back to Top
UI for WPF is Visual Studio 2017 Ready