How to set Control Template in code?
Creating template in codebehind is not a good idea, in theory one would do this by defining the ControlTemplate.VisualTree which is a FrameworkElementFactory. ControlTemplate template = new ControlTemplate(typeof(Button)); var image = new FrameworkElementFactory(typeof(Image)); template.VisualTree = image; Assigning properties is very roundabout since you need to use SetValue and SetBinding: image.SetValue(Image.SourceProperty, …); Also, about the (previously) … Read more