Pages

Thursday 20 August 2009

How to set a property to a DynamicResource reference in code

We recently needed to create a control in code and apply a style. The best reference I could find was this one at Josh Smith's blog: How to set a property to a DynamicResource reference in code

Our own example in XAML and C# looked like this:
<ItemsControl Style="{DynamicResource POSDatesTimesButtonItemsControlStyle}"

FrameworkElementFactory itemsControlElement = new FrameworkElementFactory(typeof(ItemsControl));
itemsControlElement.SetResourceReference(ItemsControl.StyleProperty,
"POSDatesTimesButtonItemsControlStyle");