Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1043
  • Last Modified:

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

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
ged125
Asked:
ged125
  • 5
  • 3
1 Solution
 
Reza RadCommented:
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
 
ged125Author Commented:
Can you give me a complete example including the code behind??
0
 
Reza RadCommented:
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
ged125Author Commented:
Reza_rad,  I tried your sample.   I get a Null Reference Exception on Line 30 of your snippet.
0
 
Reza RadCommented:
is the SiteList variable empty?
0
 
FunctionUndefinedCommented:
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
 
ged125Author Commented:
Where is the main page constructor?
0
 
ged125Author Commented:
Disregard my last question, I wasn't awake yet.
0
 
ged125Author Commented:
You nailed it!

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

Thanks a bunch!
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now