Now that the mainstream control templates and styles are referencing XAML resources, but feel that there is no C # code implementation is so flexible, now describes the code implementation ControlTemplate method:
Control renders 1 (here is image)
FrameworkElementFactory fe = new FrameworkElementFactory (typeof (image), "image");
BitmapImage bi = new BitmapImage ();
Bi. BeginInit ();
Bi. UriSource = new Uri (@ "e:chartcontrolhanyangchartcontrolimagemainbackground.jpg");
Bi. EndInit ();
Fe. SetValue (Image.sourceproperty, BI);
Control renders 2 (here is a textbox)
FrameworkElementFactory Fe2 = new FrameworkElementFactory (typeof (TextBox), "TextBox");
Fe2. SetValue (textbox.widthproperty,100.0);
Fe2. SetValue (Textbox.heightproperty, 100.0);
Encapsulate the display content to be rendered
FrameworkElementFactory f = new FrameworkElementFactory (typeof (Grid), "grid");
F.appendchild (FE);
F.appendchild (Fe2);
Control templates
ControlTemplate ct = new ControlTemplate (typeof (Button));
Ct. VisualTree = f;
Modify the button's template
Button btn = New button ();
Btn. Template = ct;