Grid in windows phone 7

前端 未结 4 995
误落风尘
误落风尘 2021-01-20 12:25

I have a grid view code below which have divided into 3 column. But i have a problem with the code is that. When multiple data is retrieved

4条回答
  •  再見小時候
    2021-01-20 12:46

    Quickhorn's answer is mostly right. The issue is you're creating one big grid instead of a row for each item in the list. Here's a simplified example of your code which uses a model object and databinding instead.

    This way you can style everything in the xaml easily and it makes things much simpler.

    Code Behind: MainPage.xaml.cs

    public partial class MainPage : PhoneApplicationPage
    {
        private ObservableCollection _schedules;
    
        public MainPage()
        {
            InitializeComponent();
    
            string[] timeSplit = new string[] { "One1", "Two2", "Three3" };
            string[] titleSplit = new string[] { "Title1", "Title2", "Title3" };
            string[] categorySplit = new string[] { "Priority", "Favourite", "Another" };
    
            _schedules = new ObservableCollection();
    
            for ( int i = 0; i < timeSplit.Length; i++ )
            {
                _schedules.Add( CreateSchedule( timeSplit[i], titleSplit[i], categorySplit[i] ) );
            }
    
            scheduleListBox.ItemsSource = _schedules;
        }
    
        private Schedule CreateSchedule( string time, string title, string category )
        {
            Schedule schedule = new Schedule
            {
                Time = time,
                Title = title,
                Category = category
            };
    
            if ( category == "Priority" )
            {
                schedule.ImageSource = "/AlarmClock;component/Images/exclamination_mark.png";
            }
            else if ( category == "Favourite" )
            {
                schedule.ImageSource = "/AlarmClock;component/Images/star_full.png";
            }
    
            return schedule;
        }
    }
    
    public class Schedule
    {
        public string Time { get; set; }
        public string Title { get; set; }
        public string Category { get; set; }
        public string ImageSource { get; set; }
    }
    

    MainPage.xaml

    
        
            
                
                    
                        
                        
                        
                    
                    
                    
                    
                        
                        
                    
                
            
        
    
    

提交回复
热议问题