开发者

Dynamically add columns to a listbox

开发者 https://www.devze.com 2022-12-28 17:04 出处:网络
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:

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&g开发者_JAVA技巧t;
            </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?


What you actually want is a grid, not a listbox. I went through similar pain of getting a grid to display in Windows Phone 7 and the good news is that you can use DataGrid from SilverlightToolkit, the bad news is that it's not optimized for the phone. You will need to look up the SilverlightToolkit source, copy the grid style and modify it to look native to the phone.

Here is my answer to my question from some time back about the datagrid.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号