将ItemsPanelTemplate设置为在WP8中动态创建ListBox

时间:2014-01-13 07:30:49

标签: windows-phone-8 listbox

我在WP8中有一个ListBox控件

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <toolkit:WrapPanel Height="120" Width="410" Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBoxItem Margin="0,0,20,0" Width="120" Height="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
        ListBoxItem 1
    </ListBoxItem>
    <ListBoxItem Margin="0,0,20,0" Width="120" Height="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
        ListBoxItem 2
    </ListBoxItem>
    <ListBoxItem Margin="0,0,10,0" Width="120" Height="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
        ListBoxItem 3
    </ListBoxItem>
</ListBox>

现在我必须在运行时添加此ListBox。我可以设法添加ListBoxItems但无法设置ItemsPanelTemplate。请帮忙。

1 个答案:

答案 0 :(得分:0)

ListBox lb = new ListBox();
ItemsPanelTemplate ddd = new ItemsPanelTemplate();
lb.ItemTemplate = ddd;

xaml中的所有内容都是类。记住它。