问题:动态创建控件,绑定了一个Style, 但是无法得到Style中Controltemplate的控件。
解决:
GroupBox gb = new GroupBox();
gb.Name = "groupbox1";
gb.Header = "MyTest1";
gb.Height = 50;
gb.Width = 100;
// 使用FindResource, 然后Apply
gb.Style = this.FindResource("MyGroupBoxWithIcon") as Style;
gb.ApplyTemplate();
// 可以直接得到Image im2
Image im2 = (Image)gb.Template.FindName("theImage", gb);