C#實(shí)現(xiàn)ControlTemplate方法
作者:佚名
這里介紹C#實(shí)現(xiàn)ControlTemplate方法,現(xiàn)在主流的控件模板和樣式是引用XAML資源,不過感覺沒有C#代碼實(shí)現(xiàn)那么靈活。
C#實(shí)現(xiàn)ControlTemplate方法
現(xiàn)在主流的控件模板和樣式是引用XAML資源,不過感覺沒有C#代碼實(shí)現(xiàn)那么靈活,介紹一下C#實(shí)現(xiàn)ControlTemplate方法:
- //控件呈現(xiàn)的顯示內(nèi)容1(這里為Image)
- FrameworkElementFactoryfe=newFrameworkElementFactory(typeof(Image),"Image");
- BitmapImagebi=newBitmapImage();
- bi.BeginInit();
- bi.UriSource=newUri(@"E:ChartControlHanYangChartControlImageMainBackground.jpg");
- bi.EndInit();
- fe.SetValue(Image.SourceProperty,bi);
- //控件呈現(xiàn)的顯示內(nèi)容2(這里為TextBox)
- FrameworkElementFactoryfe2=newFrameworkElementFactory(typeof(TextBox),"TextBox");
- fe2.SetValue(TextBox.WidthProperty,100.0);
- fe2.SetValue(TextBox.HeightProperty,100.0);
- //把要呈現(xiàn)的顯示內(nèi)容封裝起來
- FrameworkElementFactoryf=newFrameworkElementFactory(typeof(Grid),"Grid");
- f.AppendChild(fe);
- f.AppendChild(fe2);
- //控件模板
- ControlTemplatect=newControlTemplate(typeof(Button));
- ct.VisualTree=f;
- //修改Button的Template
- Buttonbtn=newButton();
- btn.Template=ct;
以上介紹C#實(shí)現(xiàn)ControlTemplate方法。
【編輯推薦】
責(zé)任編輯:佚名
來源:
博客園