我想在运行时定义一个ControlTemplate。这可能吗?我注意到ControlTemplate类的VisualTree属性。我还注意到它使用FrameworkElementFactory类。但是,我似乎无法使其正常工作。
是否可以在运行时创建ControlTemplate?
最佳答案
是的,您可以使用FrameworkElementFactory进行此操作。 Charles Petzold在“应用程序=代码+标记”的第11章中对此进行了演练,但是基本思想是为模板根元素(以及任何子元素的其他工厂)创建一个FrameworkElementFactory,创建一个ControlTemplate并设置ControlTemplate的VisualTree属性对FrameworkElementFactory的影响:
FrameworkElementFactory borderFactory = new FrameworkElementFactory(typeof(Border));
// set properties and create children of borderFactory
ControlTemplate template = new ControlTemplate();
template.VisualTree = borderFactory;
myButtonInstance.Template = template;
关于wpf - 在运行时定义WPF ControlTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/732736/