WPF in action: Dynamically set style for control

For example, I have a xaml like this:

<TabItem Header="Tab1" Style="{DynamicResource TabItemWithButton}">

I want create a new tab dynamically, & set style to “TabItemWithButton”, I will use this code:

newTab.SetResourceReference( TabItem.StyleProperty, "TabItemWithButton" );

Simple!

Actually, you can use SetResourceReference method to set many dependency properties, explore it!