Skip to main content

Microsoft Silverlight

Answered Question RC0 - databind to comboboxRSS Feed

(0)

olexiy
olexiy

Member

Member

11 points

22 Posts

RC0 - databind to combobox

Silverlight 2 RC0 project with 2 controls:

1                <ComboBox x:Name="ddl" Width="100"></ComboBox>
2                <Button x:Name="btn" Width="100"></Button>

And a bit of code  

 
1        Private Sub btn_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn.Click
2            Try
3                Dim col As New List(Of String)
4                col.Add("One")
5                col.Add("Two")
6                col.Add("Three")
7                ddl.ItemsSource = Nothing
8                ddl.ItemsSource = col
9                ddl.SelectedItem = "Two"
10           Catch ex As Exception
11   
12           End Try
13       End Sub
 

Click on a little arrow to open combo box (it will be empty). Now click the button - Silverlight will trow an unhandled exception.
Or, click a button, open combo box (so it has items) and click the button again - same exception.

An unhandled exception ('Sys.InvalidOperationException: ManagedRuntimeError error #4004 in control 'Xaml1': System.ArgumentException: Value does not fall within the expected range. at MS.Internal.XcpImports.MethodEx(IntPnt ptr, String name, CValue[]

Line 9 is the line that causes exception, although debugger goes thru it OK and in Try Catch block around these 6 lines Catch never called nor Application_UnhandledException method gets called.

If you don't open drop down in combo box with mouse but change value with keyboard, exception is not getting thrown. Opening drop down prior to selecting an item is what causes the exception.

 

.

olexiy
olexiy

Member

Member

11 points

22 Posts

Re: RC0 - databind to combobox

I have tried to use Selectedndex property instead of SelectedItem in combo box but this dosn't help.

I have also tried no to bind data but insted add into .Items collection, the same result, the same exception.

Please help, how can I select an item in the combo box progamatically?

Stephane GUNET
Stephane...

Member

Member

140 points

32 Posts

Re: RC0 - databind to combobox

I have the same problem. It seems to happen only with your list of strings. If I use a Linq query as my source for the combobox, I can use SelectedIndex without problems (I haven't tried SelectedItem, but I guess it should work too).

Tregarick
Tregarick

Member

Member

13 points

24 Posts

Re: RC0 - databind to combobox

I am getting the same issue as well.  I have a combo thats outside of a datagrid and its contents and selected item gets changed based on the currently selected row in the data grid.  So I set the combos item source and SelectedItem properties in the 'SelectionChanged' event of the datagrid.  As I hop from one row to another the combo box adjusts accordingly which is great.  However if I change the combo boxes value and then select another row I get the same issue mentioned above 'Value does not fall within the expected range' #4004.  I have tried using SelectedItem and SelectedIndex I have also tried adding strings directly to the combo's 'Items' property and binding to a list of strings but nothing seems to work.  I saw this posting though which I think is the same http://silverlight.net/forums/p/31071/98695.aspx and it seems as this is a known issue.

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24979 points

2,434 Posts

Microsoft
Answered Question

Re: RC0 - databind to combobox

Hi Olexiy,

It is a known issue.  Below is a workaround.   Please do it like this.

private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<string> myList = new List<string>();
            myList.Add("aaaa");
            myList.Add("bbbb");
            myList.Add("cccc");
            myList.Add("dddd");
            myComboBox.ItemsSource = null;
            myComboBox.ItemsSource = myList;
            myComboBox.UpdateLayout();          
            myComboBox.SelectedItem = "bbbb";
        }

Best regards,

Jonathan.

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

olexiy
olexiy

Member

Member

11 points

22 Posts

Re: RC0 - databind to combobox

Now it doesn't error out, but a drop down thing is sooo small I can't choose anything in it. Would you advise how to fix that?

yvesc
yvesc

Member

Member

20 points

16 Posts

Re: RC0 - databind to combobox

The UpdateLayout() trick fixed my issue. Thanks.

sanjay10
sanjay10

Member

Member

14 points

16 Posts

Re: RC0 - databind to combobox

 Thanks Jonathan, I spent last night 3 hours figuring out this issue, till I  Googgled and found  your reply. 

 This is a nasty bug, MSFT  should fix in Final release

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24979 points

2,434 Posts

Microsoft

Re: RC0 - databind to combobox

Hi Olexiy,

 

olexiy:
Now it doesn't error out, but a drop down thing is sooo small I can't choose anything in it. Would you advise how to fix that?

It is a known issue.  Our development team will fix it in the future released version.   Thanks.

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

jemiller
jemiller

Member

Member

445 points

237 Posts

Re: RC0 - databind to combobox

Is anyone else still running into this? As far as I can tell it wasn't fixed for RTW. I have also ran into the issue where the drop down is very small. I think what it does is set the drop down size to fit whatever is originally in the list. Then, if the number of items changes, it doesn't resize it. i.e. say the list is empty or has one item to begin with and you click to drop it down, the drop down will be small. Then, if other items are added to the list and you click the drop down again, it is still small. Does anyone know if there is a way to specify the size of the drop down part of the list explicitly? Either that, or, to somehow refresh it so that the size fits the items correctly? I don't see what the big rush on RTW was. It seems to me that Microsoft simply ignored bugs like this and released it anyway. I don't know what the point of RC0 was.

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: RC0 - databind to combobox

jemiller:

Is anyone else still running into this? As far as I can tell it wasn't fixed for RTW. I have also ran into the issue where the drop down is very small. I think what it does is set the drop down size to fit whatever is originally in the list. Then, if the number of items changes, it doesn't resize it. i.e. say the list is empty or has one item to begin with and you click to drop it down, the drop down will be small. Then, if other items are added to the list and you click the drop down again, it is still small. Does anyone know if there is a way to specify the size of the drop down part of the list explicitly? Either that, or, to somehow refresh it so that the size fits the items correctly? I don't see what the big rush on RTW was. It seems to me that Microsoft simply ignored bugs like this and released it anyway. I don't know what the point of RC0 was.

 

Exactly what my thought is on this final release. I would rather wait than getting a final buggy product.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

yvesc
yvesc

Member

Member

20 points

16 Posts

Re: RC0 - databind to combobox

You are right, this problem was not fixed in the final release. I am amazed as well ....

yvesc
yvesc

Member

Member

20 points

16 Posts

Re: RC0 - databind to combobox

Have you tried the UpdateLayout() suggested above to resize the dropdown?

sladapter
sladapter

All-Star

All-Star

17445 points

3,173 Posts

Re: RC0 - databind to combobox

 

yvesc:

Have you tried the UpdateLayout() suggested above to resize the dropdown?

 I tried, didn't do anything.

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

jemiller
jemiller

Member

Member

445 points

237 Posts

Re: RC0 - databind to combobox

I tried it and it looks like it might have fixed a problem where I wasn't able to set the SelectedItem. However, it didn't do anything with regard to the size of the drop down list.

jemiller
jemiller

Member

Member

445 points

237 Posts

Re: RC0 - databind to combobox

Does anyone know when SP1 is coming out?

 

bleaknik
bleaknik

Member

Member

2 points

3 Posts

Re: RC0 - databind to combobox

We've tried this work around using Silverlight 2.0 RTW, and we're still getting this fatal error. Our control is inside of a popup. Are there any other known workarounds? Also, does anyone know when Microsoft will be fixing this? Thanks, Jamal

... Bringing Harmony to Dissonance

walter1976
walter1976

Member

Member

2 points

6 Posts

Re: RC0 - databind to combobox

 THANKS!

 

Now i can finish my work before the deadline.

 

 

mamadero2
mamadero2

Member

Member

42 points

28 Posts

Re: RC0 - databind to combobox

 Seems to be fixed in v3. I removed this "Hack" from several places and it's working fine. 

Miguel Madero
Readify Consulting
www.miguelmadero.com
SL Tip of The Day (http://shrinkster.com/10tp)

(Please click on "Mark As Answer", if this has answered your query. Thanks.)

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities