Giving alternate color to each row of ListBox in Windows phone 7

As is generally known there is no DataGrid available under the Silverlight for Windows Phone platform at the moment. And many times we come across a situation where we need to display data as in the DataGrid with rows having alternate colors.

Here are the steps of the work around to get the desired result.

  • First we need to create new project named it as User
  • Add a new class named it as ColorConverter.cs
  • Add the Following Code to the ColorConverter.cs
namespace User

     {

     public class ColorConverter : IValueConverter   

         { 

            bool flag = false; 
            SolidColorBrush evenBrush = new SolidColorBrush(Color.FromArgb(255, 240, 248, 255)); 
            SolidColorBrush oddBrush = new SolidColorBrush(Color.FromArgb(255, 250, 235, 215));

        public object Convert(object value,
                              Type targetType,
                              object parameter, 
                System.Globalization.CultureInfo culture)

        {
            flag = !flag;
            return flag ? evenBrush : oddBrush;
        }
        public object ConvertBack(object value,
                                  Type targetType, 
                                  object parameter,
                          System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }   
}

Add a new Item named it as UserDetails.xaml In the userDetails.xaml add the following code

* as we will use the ColorConverter class to  give         alternate color to the ListBox rows we need to have the following mapping in the

Then we need to add the following code in the UserDetails.xaml




 

      
                   
         
                    
                   
                                                                                     
               
                 
                        
    
                             
          
      
                     

    

   
 
   


                                      
                            
    
  

                
                                                                                      
                                                                                      
                                                                                      
                                                         
                                                                                    
    

 

 *Add the following code to the UserDetails.xaml.cs
  Note: Here we are focusing on the Display of data in the listBox having alternate colors to the ListBox row. We are not connecting to the Database for the Data we have manually added some data and bind to the ItemsSource of the Corresponding ListBox.

    public partial class UserDetails : PhoneApplicationPage 
    {   
      // Constructor
        public UserDetails()      
        {      
          InitializeComponent();
var userDetails = new ObservableCollection   
         { 
      new User 
         {   UserName="Robert",UserState="US",UserCity="LA",UserGender="Male"},                 new User{UserName="John",UserState="US",UserCity="NY",UserGender="Male"},                 new User{ UserName="Albert",UserState="US",UserCity="LA",UserGender="Male"},                 new User{ UserName="Nick",UserState="US",UserCity="NY",UserGender="Male"}             };
            lsbUser.ItemsSource = userDetails;
        }        }
      ///      /// User Class     ///   
   public class User     {
        private string userName; 
        private string userState; 
        private string userCity;  
        private string userGender;
        public string UserName     
           {        
            get    

              {   
               return userName; 
     }         
    set             
       {
         userName = value;  
           }     
      }
        public string UserState 
           { 
             get  
              {
                return userState;  
                   }      
                    set 

             {  
               userState = value; 
               }  
       }
        public string UserCity 
         {
          get 
              { 
               return userCity;
                 }
                  set 
                {  
                 userCity = value;    
                           }  
                }
        public string UserGender     
         {  
           get 
             { 
                  return userGender;
                } 
             set      
       {              
       userGender = value;  
           }  
       }
150 150 Burnignorance | Where Minds Meet And Sparks Fly!