Solved

Silverlight ComboBox "SelectionChanged" Event

Posted on 2010-08-18
8
2,177 Views
Last Modified: 2013-11-12
Once again, I am new to Silverlight and trying my best to learn it quickly.  I have a basic form that has a comboBox (comboBox1 in the example).  This comboBox has items that are populated by a webservice, and that is working great.  I am used to ASPX pages where I can use the "SelectionChanged" event and it actually fires when the selection changes.  With Silverlight, the "SelectionChanged" event seems to fire on the inital load on the applicaiton as well.

Anyway, on the code behind example below, I am getting a null reference exception on line 4.  The compiler doesn't seem to know anything about the combobox.  How do I access the "SelectedIndex" property of the XAML ComboBox from the codebehind page?
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            if (this.comboBox1.SelectedIndex != 0)
            {
                Service1SoapClient wRef = new Service1SoapClient();
                wRef.GetSiteDetailsCompleted += new EventHandler<GetSiteDetailsCompletedEventArgs>(wRef_GetSiteDetailsCompleted);
                string site = "MySite";
                //wRef.GetSiteDetailsAsync(comboBox1.SelectedValue.ToString());
                wRef.GetSiteDetailsAsync(site);
            }

        }

Open in new window

0
Comment
Question by:ged125
  • 4
  • 4
8 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33470914
try

combobox1.selectedIndex !=0 and are you using MVVM pattern?
0
 
LVL 6

Author Comment

by:ged125
ID: 33470928
you mean remove the "this" from line 4?  Yes I did try that.   And I don't know what MVVM is. :)
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33470949
what kind of pattern are you using is it

MVC, MVVM or MVP
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:ged125
ID: 33470958
I am going to sound completey stupid here.  But how do I tell?  :)  I am using VS 2010, I did a Silverlight 4.0 Application, a Asp.net 3.5 web service middle tier, and asp.net 4.0 website.

As I said, I am new to Silverlight, but I have written ASPX before.
0
 
LVL 18

Accepted Solution

by:
Anil Golamari earned 500 total points
ID: 33470970
That error may occur because of two reasons
1) combo box is not instantiated
2) If you have issue with data binding to the combo box.

So take a look at the link below and see whether it fixed your issue.

http://forums.silverlight.net/forums/p/135593/303116.aspx 
0
 
LVL 6

Author Comment

by:ged125
ID: 33471078
Lucky85,  it appears you were correct about the combo Box not being instantiated.  Before I award the points, I just have a quick question.   When writing code behind an ASPX, just the simple fact that you have a drop down list on the ASPX page is enough to instantiate the object.  What's the difference in XAML code behind?
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33471145
http://forums.silverlight.net/forums/p/160806/361082.aspx

Take a look at the above link which will help you understand the page life cycle of silverlight.
0
 
LVL 6

Author Closing Comment

by:ged125
ID: 33471148
ComboBox was not instantiated.  Added the following

ComboBox cb  = sender as ComboBox;
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 45
asp web application 3 37
Where does legacy ASP.NET initialize language? 2 56
Dictionary and array of [N] size - performance tuned 12 40
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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…

735 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