wp7下如何动态创建ListBox
例如我们可以这样设定Listbox
1 <ListBox x:Name="LB1">
2 <ListBox.ItemTemplate>
3 <DataTemplate>
4 <StackPanel Width="50">
5 <TextBlock Text="{Binding Name}"></TextBlock>
6 </StackPanel>
7 </DataTemplate>
8 </ListBox.ItemTemplate>
9 </ListBox>
TextBlock textBlock = new TextBlock();
textBlock.Text = "测试数据";
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(textBlock);
grid1.Children.Add(textBlock);
grid1.Children.Add(stackPanel);
<Style x:Key="horizontalListBoxStyle" TargetType="ListBox">
<!--容器模板-->
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"<!--横向-->
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<!-- item模板 -->
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<!-- 这里设置自己定义的控件和显示-->
<StackPanel Orientation="Horizontal">
<TextBlock Padding="5,0,5,0"
Text="{Binding FirstName}" />
<TextBlock Text="{Binding LastName}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>