views:

295

answers:

1

In silverlight, I'm creating a listbox at runtime. The listbox displays on the page okay but the items aren't selectable - I don't understand why? Am I doing something wrong? Here's my code:

C#

public partial class MainPage : UserControl
{

    public MainPage()
    {
        InitializeComponent();

        ListBox lb = GetListbox();
        LayoutRoot.Children.Add(lb);
    }

    private ListBox GetListbox()
    {
        ListBox lb = new ListBox();
        lb.Items.Add("Option 1");
        lb.Items.Add("Option 1");
        return lb;
    }

}

VB

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()

        Dim lb As ListBox = GetListbox()
        LayoutRoot.Children.Add(lb)
    End Sub

    Private Function GetListbox() As ListBox
        Dim lb As New ListBox
        lb.Items.Add("Option 1")
        lb.Items.Add("Option 1")
        Return lb
    End Function

End Class
+2  A: 

It is because both items are named "Option 1". The listbox can not tell the two items apart because to .NET the two string items are identical. If you try using two different strings my guess is that it will work just fine:

lb.Items.Add("Option 1"); 
lb.Items.Add("Option 2");
Jakob Christensen
Thanks, but unfortunately, this didn't work.
rip
Too bad. Your code works fine for me though.
Jakob Christensen
worked for me in silverlight 4
Jeff Wilcox
Your right – it does work! My problem was that I had imported Telerik.Windows.Controls and therefore the ListBox was a telerik listbox which must have different behaviour. Thanks for your help.
rip