How to style RadTreeViewItem in code behind?

3 posts, 1 answers
  1. Greg
    Greg avatar
    34 posts
    Member since:
    Sep 2012

    Posted 24 May 2016 Link to this post

    Hello, can someone tell me how to style a RadTreeViewItem purely in code behind?

    I did read this article:

    but I don't want to use XAML at all and I don't know how to access those properties in code.

  2. Answer
    Dinko avatar
    1037 posts

    Posted 26 May 2016 Link to this post

    Hi Greg,

    Let me first start with that it is not a good practice to specify a custom style in code behind. If you still consider setting a custom style of the RadTreeViewItems in code behind you can take a look at the sample example below.
    public MainWindow()
        Style customStyle = new Style();
        customStyle.TargetType = typeof(RadTreeViewItem);
        Setter setter = new Setter();
        setter.Property = TextBlock.ForegroundProperty;
        setter.Value = Brushes.Green;
        Setter setter2 = new Setter();
        setter2.Property = RadTreeViewItem.IsExpandedProperty;
        setter2.Value = true;
        this.myTree.ItemContainerStyle = customStyle;
    By setting the custom style to the ItemContainerStyle property of the RadTreeView you can applied it to all tree items in the tree.

    Another approach that you can try is to create a custom ResourceDictionary where you specify the custom style and applied to the RadTreeViewItems.

    You can take a look at the following MSDN resources which could be in handy for your case. Hope this information is useful.

    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. Greg
    Greg avatar
    34 posts
    Member since:
    Sep 2012

    Posted 26 May 2016 in reply to Dinko Link to this post

    Thank you, this has been very helpful!
Back to Top