Solved

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

Posted on 2010-08-16
9
1,041 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

622 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