現(xiàn)在主流的控件模板和樣式是引用XAML資源,不過感覺沒有c#代碼實(shí)現(xiàn)那么靈活,現(xiàn)介紹一下代碼實(shí)現(xiàn) ControlTemplate的方法:
以下是引用片段: //控件呈現(xiàn)的顯示內(nèi)容1(這里為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);
//控件呈現(xiàn)的顯示內(nèi)容2(這里為TextBox) FrameworkElementFactory fe2 = new FrameworkElementFactory(typeof(TextBox), "TextBox"); fe2.SetValue(TextBox.WidthProperty,100.0); fe2.SetValue(TextBox.HeightProperty, 100.0);
//把要呈現(xiàn)的顯示內(nèi)容封裝起來 FrameworkElementFactory f = new FrameworkElementFactory(typeof(Grid), "Grid"); f.AppendChild(fe); f.AppendChild(fe2);
//控件模板 ControlTemplate ct = new ControlTemplate(typeof(Button)); ct.VisualTree = f;
//修改Button 的Template Button btn = new Button(); btn.Template = ct; |
|