<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding fieldA}" />
</DataTemplate>
我想以编程方式完成相应的上述XAML(XAML还有更多,我只显示了相关的位)。到目前为止我有:
DataTemplate newDataTemplate = new DataTemplate();
TextBlock newTextBlock = new TextBlock();
newTextBlock.SetBinding(TextBlock.TextProperty, new Binding("fieldA"));
newTextBlock.Name = "txt";
那么我现在如何将TextBlock添加到DataTemplate ...即我想做类似的事情:
newDataTemplate.children.Add(TextBlock)
答案 0 :(得分:7)
var newTextBlock = new FrameworkElementFactory(typeof(TextBlock));
newTextBlock.Name = "txt";
newTextBlock.SetBinding(TextBlock.TextProperty, new Binding("fieldA"));
DataTemplate newDataTemplate = new DataTemplate(){VisualTree = newTextBlock};
我认为你应该看看这个问题。
How do I create a datatemplate with content programmatically?