Carl Rippon
Carl Rippon

Reputation: 4673

Listbox items not selectable

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

Upvotes: 0

Views: 1187

Answers (1)

Jakob Christensen
Jakob Christensen

Reputation: 14956

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");

Upvotes: 2

Related Questions