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

How to style RadTreeViewItem in code behind?

2 Answers 274 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 24 May 2016, 11:08 AM

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

I did read this article: http://docs.telerik.com/devtools/wpf/controls/radtreeview/styles-and-templates/styling-and-appearance-styling-radtreeviewitem#modifying-the-default-style

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

2 Answers, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 26 May 2016, 10:09 AM
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()
{
    InitializeComponent();
 
    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;
 
    customStyle.Setters.Add(setter);
    customStyle.Setters.Add(setter2);
 
    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.

Regards,
Dinko
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Greg
Top achievements
Rank 1
answered on 26 May 2016, 10:48 AM
Thank you, this has been very helpful!
Tags
TreeView
Asked by
Greg
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Greg
Top achievements
Rank 1
Share this question
or