Solved

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

Posted on 2010-08-16
9
1,035 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

20 Experts available now in Live!

Get 1:1 Help Now