如何以编程方式将控件添加到数据模板?
例如。下面我创建了TextBlock和DataTemplate。
TextBlock text = new TextBlock();
DataTemplate template = new DataTemplate();现在我需要向DataTemplate添加TextBlock。如何做到这一点?
我知道在后台代码中添加数据模板还有其他方法1.在XAML中创建数据模板并将其加载到后台代码2.使用XamlParser创建和添加。
但是我需要按照我在示例中展示的方式来做。
我需要一些帮助。
发布于 2013-06-14 18:49:48
尽管XAML可以工作,但在官方看来,它已被弃用,相反,建议以编程方式创建模板的方法是使用XamlReader类的load方法从字符串或内存流加载Archedius's method,如下所示……
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
发布于 2011-04-08 16:22:03
您必须首先声明一个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;希望它能有所帮助!:)
发布于 2014-08-21 21:58:00
我知道这是一种变通办法,但我在代码项目(http://www.codeproject.com/Tips/808808/Create-Data-and-Control-Templates-using-Delegates)中发布了一个技巧,允许您使用委托创建数据模板。例如:
TemplateGenerator.CreateDataTemplate(() => new TextBox());这将足以创建一个数据模板来创建一个新的文本框。如果你也想要一个绑定,可以这样写:
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的方法。
https://stackoverflow.com/questions/5471405
复制相似问题