在后面的代码中创建DataTemplate

如何以编程方式向datatemplates添加控件?

例如。 下面我创建了TextBlock和DataTemplate。

TextBlock text = new TextBlock(); DataTemplate template = new DataTemplate(); 

现在我需要将TextBlock添加到DataTemplate。 怎么做到这一点?

我知道在代码中有addind数据模板的其他方法1.在XAML中创建数据模板并将其加载到后面的代码上2.使用XamlParser创建和添加

但我需要按照我在例子中展示的方式做。

需要一些帮助。

您首先要声明一个DataTemplate:

 DataTemplate template = new DataTemplate { DataType = typeof(< Type of the object the template refers>) }; 

然后以这种方式声明像StackPanel这样的布局面板

 FrameworkElementFactory stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel)); stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical); 

最后将TextBlock片段附加到它:

 FrameworkElementFactory title = new FrameworkElementFactory(typeof(TextBlock)); title.SetBinding(TextBlock.TextProperty, new Binding("< name of your binding >")); stackPanelFactory.AppendChild(title); 

为了显示以这种方式创建的StackPanel,您必须将它附加到VisualTree:

 template.VisualTree = stackPanelFactory; 

希望能帮助到你! 🙂

虽然Archedius的方法有效,但官方它已被弃用,而是建议以编程方式创建模板的方法是使用XamlReader类的Load方法从字符串或内存流加载XAML,如下所示…

 public DataTemplate Create(Type type) { StringReader stringReader = new StringReader( @" <" + type.Name + @" Text=""{Binding " + ShowColumn + @"}""/> "); XmlReader xmlReader = XmlReader.Create(stringReader); return XamlReader.Load(xmlReader) as DataTemplate; } 

官方专线来自msdn: http : //msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory.aspx

Fredrik Hedblad的post中的代码示例: XamlReader生成DataTemplate的问题

我知道这是一个解决方法,但我在代码项目( http://www.codeproject.com/Tips/808808/Create-Data-and-Control-Templates-using-Delegates )中发布了一个提示,允许您使用委托创建数据模板。 例如:

 TemplateGenerator.CreateDataTemplate(() => new TextBox()); 

这足以创建一个创建新文本框的datatemplate。 如果你想要一个绑定,它可以写成:

 TemplateGenerator.CreateDataTemplate ( () => { var result = new TextBox(); result.SetBinding(TextBox.TextProperty, "PathForTheBinding"); return result; } ); 

TemplateGenerator的代码如下:

 ///  /// Class that helps the creation of control and data templates by using delegates. ///  public static class TemplateGenerator { private sealed class _TemplateGeneratorControl: ContentControl { internal static readonly DependencyProperty FactoryProperty = DependencyProperty.Register("Factory", typeof(Func), typeof(_TemplateGeneratorControl), new PropertyMetadata(null, _FactoryChanged)); private static void _FactoryChanged(DependencyObject instance, DependencyPropertyChangedEventArgs args) { var control = (_TemplateGeneratorControl)instance; var factory = (Func)args.NewValue; control.Content = factory(); } } ///  /// Creates a data-template that uses the given delegate to create new instances. ///  public static DataTemplate CreateDataTemplate(Func factory) { if (factory == null) throw new ArgumentNullException("factory"); var frameworkElementFactory = new FrameworkElementFactory(typeof(_TemplateGeneratorControl)); frameworkElementFactory.SetValue(_TemplateGeneratorControl.FactoryProperty, factory); var dataTemplate = new DataTemplate(typeof(DependencyObject)); dataTemplate.VisualTree = frameworkElementFactory; return dataTemplate; } ///  /// Creates a control-template that uses the given delegate to create new instances. ///  public static ControlTemplate CreateControlTemplate(Type controlType, Func factory) { if (controlType == null) throw new ArgumentNullException("controlType"); if (factory == null) throw new ArgumentNullException("factory"); var frameworkElementFactory = new FrameworkElementFactory(typeof(_TemplateGeneratorControl)); frameworkElementFactory.SetValue(_TemplateGeneratorControl.FactoryProperty, factory); var controlTemplate = new ControlTemplate(controlType); controlTemplate.VisualTree = frameworkElementFactory; return controlTemplate; } } 

它也有ControlTemplates的方法。