About C # code implementation ControlTemplate

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.