Dynamically add columns to a listbox

Posted by mgroves on Stack Overflow See other posts from Stack Overflow or by mgroves
Published on 2010-05-02T15:42:54Z Indexed on 2010/05/02 15:47 UTC
Read the original article Hit count: 311

I'm brand new to Windows Phone 7 development, and almost as equally new to Silverlight. I have a ListBox with a DataTemplate, StackPanel, and TextBlocks like so:

    <ListBox Height="355" HorizontalAlignment="Left" Margin="6,291,0,0" Name="detailsList" VerticalAlignment="Top" Width="474" Background="#36FFFFFF">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="50" Text="{Binding Ticker}" />
                    <TextBlock Width="50" Text="{Binding Price}" />
                    <TextBlock Width="50" Text="{Binding GainLoss}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

I have some C# code to populate it:

    var info = new List<StockInfo>();
    info.Add(new StockInfo { Ticker = "C", Price = "5.18", GainLoss = "10" });
    info.Add(new StockInfo { Ticker = "WEN", Price = "4.18", GainLoss = "12" });
    info.Add(new StockInfo { Ticker = "CBB", Price = "5.22", GainLoss = "210" });
    detailsList.ItemsSource = info;

And that all works fine. My question is: how do I add/remove additional 'textblocks' to the listbox dynamically (at runtime)? Also, how do I put column headers on the list box?

© Stack Overflow or respective owner

Related posts about windows-phone-7

Related posts about Silverlight