Solved

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

Posted on 2010-08-16
9
1,036 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain "Multi-Tenant Services" 5 65
Help with Query not working in client's PC 1 36
ADO.NET ENTITY DATA MODEL 3 32
Capture logon name 13 48
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now