Adding nodes in code with custom sized image

3 posts, 1 answers
  1. Anne Lyon
    Anne Lyon avatar
    87 posts
    Member since:
    Jan 2010

    Posted 06 Sep 2010 Link to this post

    I have a treeview where I add nodes dynamically in code (LoadOnDemand). This works fine, but I need to "label" the different nodes with different images depending on what kind of node it is. This also works fine - as long as the images are always 16x16 pixels. If they are not (sometimes I need images of 16x32) then the images are resized to 16px wide.
    I tried to make a template to handle images of this size and created a ControlTemplate with an Image with Height and Width set. But when doing this I don't seem to be able to display the Text of the node (in the Header property). Adding a TextBlock to the template only displays an empty string. What is worse, the expand button disappears. So I think my solution is a dead end...

    Now can anybody explain to me how to create a template (or other solution), that custom set the size of the image for a RadTreeViewItem but leaves the rest "as is" and that can be set/applied in code??

  2. Answer
    Hristo avatar
    352 posts

    Posted 09 Sep 2010 Link to this post

    Hi Anne Lyon,

    There are several approaches you can try:
    1. Bind the width and height properties from data template to appropriate properties in your view model.
    2. Use template selector to choose which template to be used for each item.
    I'm attaching sample project demonstrating both solutions.

    Hope this helps. Please let us know if you need more info.

    Hristo Milyakov
    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. DevCraft R3 2016 release webinar banner
  4. Anne Lyon
    Anne Lyon avatar
    87 posts
    Member since:
    Jan 2010

    Posted 10 Sep 2010 Link to this post

    Brilliant! Used the ViewModel solution and it works very well. Thanks!
Back to Top