简体   繁体   中英

How to add rows to gridview dynamically in windows phone 7.1 application?

in my window phone 7.1 application(silverlight application, c# language using VS 2010 express for windows phone) i created gridview to show my data(search results) in table format. I created a class and bound that gridview to that class succussfully. this is my xaml code:

<phone:PhoneApplicationPage.Resources>
        <local:searchResultItemModel x:Key="searchResultIM"/>
    </phone:PhoneApplicationPage.Resources>   


  <gridView:GridView x:Name="GridView1"  CellSpacing="1" RowSpacing="1" SelectedItemChanged="GridViewSelectedItemChanged" Margin="26,16,25,22" ItemsSource="{Binding Source={StaticResource searchResultIM}, Path=Data}">

But i want to add rows dynamically. As this is static i cant able to add rows to it. Is anyother way to add rows dynamically . Could anybody help me please?

You have to use the ListBox Control and template it to your liking. The ListBox also includes bunch of features like UIVitualization that will help with bigger sets of data.

As said you can use the ListBox with a datatemplate to control the look of each row. Example:

<ListBox ItemsSource="{Binding Source={StaticResource searchResultIM}, Path=Data}" ItemTemplate="{StaticResource SearchItemTemplate}" />

Put the above ListBox element inside your LayoutRoot (usually a Grid control) in your Phone page. In the ListBox you will refer to the ItemTemplate to use (defined as DataTemplate) which you define in your resources section, example:

<DataTemplate x:Name="SearchItemTemplate">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock Text="{Binding YourDataProperty1}" />
    <TextBlock Text="{Binding YourDataProperty2}" Grid.Column="1" />
</Grid>
</DataTemplate>

The datacontext for the template will be the type of object you get in your results. So YourDataProperty1 etc could be a property on that resultobject.

Hope it helps! /Anders

Building a DataGrid Control for Silverlight for Windows Phone

http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx

This suits the way to create a dynamic table perfectly, The problem in using list box is if the contents goes beyind windows phone, its not easy then to create table using listbox. This grid control has lot of features that would help a newbie like me. Its totally intuitive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM