关于C#代码实现ControlTemplate
现在主流的控件模板和样式是引用XAML资源,不过感觉没有c#代码实现那么灵活,现介绍一下代码实现 ControlTemplate的方法:以下是引用片段: Iix:GH5a@
//控件呈现的显示内容1(这里为Image)
FrameworkElementFactory fe = new FrameworkElementFactory(typeof(Image), "Image");
BitmapImage bi = new BitmapImage();
bi.BeginInit(); &V5|3L)Q*_ ATe
bi.UriSource = new Uri(@"E:ChartControlHanYangChartControlImageMainBackground.jpg");
bi.EndInit(); r3`$}6YcOm!DL
)V;Q\ W`!N~
fe.SetValue(Image.SourceProperty, bi); W5R7WD&B H5j
//控件呈现的显示内容2(这里为TextBox) b Rf T(s
FrameworkElementFactory fe2 = new FrameworkElementFactory(typeof(TextBox), "TextBox");
fe2.SetValue(TextBox.WidthProperty,100.0); "y1@+V+K@PM`}4O+lZ
fe2.SetValue(TextBox.HeightProperty, 100.0);
jff'v`RN
//把要呈现的显示内容封装起来 (k(P_7@$d%C L%~
FrameworkElementFactory f = new FrameworkElementFactory(typeof(Grid), "Grid");
f.AppendChild(fe);
f.AppendChild(fe2); (dm ^B1Py&{ B
I:ph3E[_
//控件模板 "Z1Xhg]a"q^y
ControlTemplate ct = new ControlTemplate(typeof(Button)); u!~I,P|{
ct.VisualTree = f;
2u!kN#`-e"X4k
//修改Button 的Template Ko8o,T D~ xa0g
Button btn = new Button(); ,Z#H0ClL"q1n&JV
btn.Template = ct;
页:
[1]