简体   繁体   中英

Grid Datasource in WP8

Im really new to WP8... and I still do to understand some mechanism i used to face off when i developed in aspnet... For Example, datasource, databind,...

I do not understand how to visualize some element in a grid:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Loaded="ContentPanel_Loaded">

      <TextBlock x:Name="xx" .../>  
</Grid>

I just want to repeat the textblock n times... in this moment the only way i manage to do this, is doing somenthing like this

foreach (....)
{
     TextBlock tb = new TextBlock();
     tb.Text = p.Name;

     ContentPanel.Children.Add(tb);
}

But i suppose this is not the right way... any suggestion? Thanx

Here's the xaml

<ListBox x:Name="NameList">
   <ListBox.ItemTemplate>
        <DataTemplate>
           <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding Path=Name}" Width="200"/>

           </StackPanel>
         </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

cs

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StackOverFlowTestApp.Resources;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.UserData;

namespace StackOverFlowTestApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        private SaveAppointmentTask saveAppointmentTask;
        private List<int> listMinutes = new List<int>();
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            List<User> list = new List<User>();
            for (int i = 0; i < 10; i++) {
                User user = new User();
                user.name = "Anobik"+i;
                list.Add(user);
            }
            NameList.ItemsSource = list;
        }


    }


    public class User {
        public string name { get; set; }
    }


}

Try with it the simplest way. but actually the concept of binding extends to MVVM so you can read from the article

MVVM windows phone 8

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