3
<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)
4

1 に答える 1

7
var newTextBlock = new FrameworkElementFactory(typeof(TextBlock));
newTextBlock.Name = "txt";
newTextBlock.SetBinding(TextBlock.TextProperty, new Binding("fieldA"));
DataTemplate newDataTemplate = new DataTemplate(){VisualTree = newTextBlock};

私はあなたがこの質問を見るべきだと思います。

プログラムでコンテンツを含むデータテンプレートを作成するにはどうすればよいですか?

于 2012-04-29T06:27:42.107 に答える