我将尝试为ListBox编写一个ItemTemplate模板,但无法实现。我知道在XAML中可以这样写:
:
<ListBox x:Name="listbox" BorderThickness="0" Margin="6" Height="400">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Margin="0" Background="Red" Foreground="White" FontSize="18" Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是,当我试图以编程方式获得上述结果时,我遇到了一个问题,即绑定TextBox.TextProperty
:
var textblock = new FrameworkElementFactory(typeof(TextBlock));
// Setting some properties
textblock.SetValue(TextBlock.TextProperty, ??);
var template = new ControlTemplate(typeof(ListBoxItem));
template.VisualTree = textblock;
请帮我解决这个问题。我在网上找不到任何相关信息。
提前感谢您的帮助。