Skip to main content

Microsoft Silverlight

Answered Question SelectedIndex causes exception on ComboBox - Silverlight 2RSS Feed

(0)

tyrel
tyrel

Member

Member

16 points

25 Posts

SelectedIndex causes exception on ComboBox - Silverlight 2

Simple really, create two combo boxes. Make the first one populate data in the second setting the ItemsSource to a list, then select the first item in the list using SelectedIndex = 0. First time this works. Change the selection on the second combo box, then change the selection on the first again and hey presto, you get an exception.

http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=SilverlightComboBox&DownloadId=5948

I should add that I assume it's a bug, not really anything unusual about what I am trying to do there. Does anyone know if there are any workarounds for this problem? And has it been fixed in SL 3? Thanks.

RogerGu
RogerGu

Participant

Participant

887 points

260 Posts

Re: SelectedIndex causes exception on ComboBox - Silverlight 2

I tried a few times while debugging, pretty sure I followed the instructions. I did not get an exception thrown.

tyrel
tyrel

Member

Member

16 points

25 Posts

Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

Weird. Using the sample I provided? I will get someone else here to try it. I have had weird problems before with Silverlight where things don't work, reboot and hey presto, they work again. I got the error in an application I am working on, then created the sample to see if it's related to some other code in there or not. Literally select item in combo box 1, change selection in combo box 2, change selection in combo box 1 and exception. I have no way of getting around it at this point. I guess I will try a reboot and see if that works, seems like the typical customer support response though. "Sir, have you rebooted your computer yet?". :-)

tyrel
tyrel

Member

Member

16 points

25 Posts

Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

I just had a co-worker try the sample on their computer and he got the exception to occur as well. The error I get seems to be the generic Silverlight error where it goes to JScript onSilverlightError function. I hope I don't have to create a custom control drop down box to get around this, that would seem like a whole lot of work to do to get around a problem with a build in control. Anyone else with some ideas?

RogerGu
RogerGu

Participant

Participant

887 points

260 Posts

Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

Yes, with the sample provided. Sorry I am no help. I'll try a different pc later today if I get the chance.

tyrel
tyrel

Member

Member

16 points

25 Posts

Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

Thanks mate. This is driving me nuts. :-) It's not like I am trying to do something totaly weird here. I do wonder if it has something to do with the version of Silverlight, you don't have 3 installed do you?

shailesh_buzz
shailesh...

Member

Member

99 points

43 Posts

Re: Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

 Hi,

Similar kind of problem i'm facing. I'm having two comboboxes. on select of first other combobox get populated. so on change of first combobox second one get re-populated with new values. after populating i'm setting its SelectedIndex to 0 so that it points to 1st item in dropdown.

here every thing works fine. But when i open 2nd dropdown after populate; and changing its value or without changing its value, if i select other value in 1st drop down then it populates 2nd dropdown again. This time if i set SelectedIndex property of 2nd dropdown throws some exception. I'm not getting debug point in app.xaml.cs's Unhandleexception event but application_end event get fired and it closes application. On UI silverlight part disappears.

 code for binding :

 ddlClasses.ItemsSource = null;
                ddlClasses.ItemsSource = classList;
                ddlClasses.DisplayMemberPath = "Name";
                ddlClasses.IsEnabled = true;
                ddlClasses.SelectedIndex = 0;

if SelectedIndex is not set then it works fine.

 does any one have found any solution for this?

StefanWick
StefanWick

Contributor

Contributor

2864 points

438 Posts

Microsoft
Answered Question

Re: Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

We have addressed this for Silverlight 3. Can you try the Silverlight 3 Beta to verify this no longer repros?

In Silverlight 2 you should be able to work around this by calling UpdateLayout() before setting the SelectedIndex.

Let me know if this doesn't help.

Thanks, Stefan Wick

Microsoft Silverlight | http://blogs.msdn.com/swick/

shailesh_buzz
shailesh...

Member

Member

99 points

43 Posts

Re: Re: Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

 Thanks,

Using UpdateLayout before SelectedIndex set works.

tyrel
tyrel

Member

Member

16 points

25 Posts

Re: Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

Thanks Stefan! I will add the code. We cannot use 3 at the moment. Don't think it will be released by the time we have to release.

rajkra
rajkra

Member

Member

33 points

36 Posts

Re: Re: Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

How to do UpdateLayout() because it returns false

Raja

tyrel
tyrel

Member

Member

16 points

25 Posts

Re: Re: Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

Not sure exactly what the problem is you are facing, but all I did was call UpdateLayout before setting the selected index, like this:

UserStatusComboBox.ItemsSource = userStatuses;
UserStatusComboBox.UpdateLayout();
UserStatusComboBox.SelectedIndex = 0;

tyrel
tyrel

Member

Member

16 points

25 Posts

Re: Re: Re: SelectedIndex causes exception on ComboBox - Silverlight 2

Sorry for the long delay Stefan, but we upgraded to Silverlight 3 now that it has been released and I tried the sample again. It works! So looks like the problem has been fixed in version 3.

Thanks.

 PS. Now can we get printing from SL? :-)

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities