Solved

How do I populate a ComboBox from XAML code behind page?

Posted on 2010-08-16
9
1,037 Views
Last Modified: 2013-12-17
I am new to Silverlight, so I'll apologize up front for the simple question.  How in the world do you populate the items in a ComboBox from the C# code behind page?  I have always been able to do "combobox.items.add(item);" without a problem.  But now that I am trying to start using Silverlight, it's not working.  I am getting a Null reference exception at line 32 of the code below.  All of the documentation I have read says that this should work.  I'm at a loss.  Please help.

Details:
.net 4.0
VS2010
Silverlight 4.0

Thanks in advance!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Configuration;
using SilverlightApplication.ServiceReference1;

namespace SilverlightApplication
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            Service1SoapClient wRef = new Service1SoapClient();
            wRef.GetSiteListCompleted += new EventHandler<GetSiteListCompletedEventArgs>(wRef_GetSiteListCompleted);
            wRef.GetSiteListAsync();
        }

        void wRef_GetSiteListCompleted(object sender, GetSiteListCompletedEventArgs e)
        {

            Site[] siteList = e.Result.ToArray();
            foreach (Site site in siteList)
            {
                comboBox1.Items.Add(site.SiteCode.ToString());
            }
        }



    }
}

Open in new window

0
Comment
Question by:ged125
  • 5
  • 3
9 Comments
 
LVL 30

Expert Comment

by:Reza Rad
ID: 33451491
Why you don't set DataContext property of combo box? like this:
comboBox1.DataContext=something;


also you need to set SelectedValuePath and DisplayMemberPath in xaml, like this:

<ComboBox Name="comboBox1" Width="150" SelectedValuePath="ID" DisplayMemberPath="Name" ></ComboBox>


0
 
LVL 6

Author Comment

by:ged125
ID: 33451497
Can you give me a complete example including the code behind??
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 33451548
this is code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Configuration;
using SilverlightApplication.ServiceReference1;

namespace SilverlightApplication
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            Service1SoapClient wRef = new Service1SoapClient();
            wRef.GetSiteListCompleted += new EventHandler<GetSiteListCompletedEventArgs>(wRef_GetSiteListCompleted);
            wRef.GetSiteListAsync();
        }

        void wRef_GetSiteListCompleted(object sender, GetSiteListCompletedEventArgs e)
        {

            Site[] siteList = e.Result.ToArray();
            comboBox1.DataContext=sireList as IEnumerable<Site>;
            
        }



    }
}

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 6

Author Comment

by:ged125
ID: 33451914
Reza_rad,  I tried your sample.   I get a Null Reference Exception on Line 30 of your snippet.
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 33452136
is the SiteList variable empty?
0
 
LVL 2

Accepted Solution

by:
FunctionUndefined earned 500 total points
ID: 33452468
I believe u deleted, unintentionally, a very important line from your MainPage() constructor, which should be something like
                  // Required to initialize variables
                  InitializeComponent();
Right now your combo is null/uninitialized since this method is missing. It is normally generated by the compiler, inside this method being hidden the C# code translation of your XAML page. Try to write this lines again in the constructor at the beginning; If it does not work, re-create the page.
0
 
LVL 6

Author Comment

by:ged125
ID: 33454835
Where is the main page constructor?
0
 
LVL 6

Author Comment

by:ged125
ID: 33454895
Disregard my last question, I wasn't awake yet.
0
 
LVL 6

Author Closing Comment

by:ged125
ID: 33454914
You nailed it!

From my original code snippet, I added the InitializeComponent(); method under MainPage() and bingo!

Thanks a bunch!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question