如何以编程方式向datatemplates添加控件?
例如。下面我创建了TextBlock和DataTemplate。
TextBlock text = new TextBlock();
DataTemplate template = new DataTemplate();
现在我需要将TextBlock添加到DataTemplate。怎么做到这一点?
我知道在代码背后还有其他addind数据模板的方法 1.在XAML中创建数据模板并将其加载到代码后面 2.使用XamlParser创建和添加
但我需要按照我在示例中展示的方式进行。
需要一些帮助。
答案 0 :(得分:29)
首先要声明一个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;
希望它有所帮助! :)
答案 1 :(得分:29)
虽然Archedius的方法有效,但官方它已被弃用,而是建议以编程方式创建模板的方法是使用XamlReader类的Load方法从字符串或内存流加载XAML,如下所示......
public DataTemplate Create(Type type)
{
StringReader stringReader = new StringReader(
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<" + type.Name + @" Text=""{Binding " + ShowColumn + @"}""/>
</DataTemplate>");
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的帖子中的代码示例:Problems with XamlReader generating DataTemplate
答案 2 :(得分:5)
我知道这是一种解决方法,但我在代码项目(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的代码如下:
/// <summary>
/// Class that helps the creation of control and data templates by using delegates.
/// </summary>
public static class TemplateGenerator
{
private sealed class _TemplateGeneratorControl:
ContentControl
{
internal static readonly DependencyProperty FactoryProperty = DependencyProperty.Register("Factory", typeof(Func<object>), typeof(_TemplateGeneratorControl), new PropertyMetadata(null, _FactoryChanged));
private static void _FactoryChanged(DependencyObject instance, DependencyPropertyChangedEventArgs args)
{
var control = (_TemplateGeneratorControl)instance;
var factory = (Func<object>)args.NewValue;
control.Content = factory();
}
}
/// <summary>
/// Creates a data-template that uses the given delegate to create new instances.
/// </summary>
public static DataTemplate CreateDataTemplate(Func<object> 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;
}
/// <summary>
/// Creates a control-template that uses the given delegate to create new instances.
/// </summary>
public static ControlTemplate CreateControlTemplate(Type controlType, Func<object> 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的方法。