简体   繁体   中英

Wp7 ListBox ItemSource ObservableCollection IndexOUtofRange and Items are not updated

I have the following issue: I am creating a Windows Phone 7 application and I am using a ListBox which is bound to an ObservableCollection people. The implementation of this you see below:

public class Person
{
    private string _id { get; set; }
    private string _name { get; set; }


    public Person(string Id, string Name, string Title)
    {
        _id = Id;
        _name = Name;
    }

    public string Id
    {

        get { return _id; }

        set
        {

            _id = value;

            FirePropertyChangedEvent("Id");

        }
    }

    public string Name
    {

        get { return _name; }

        set
        {

            _name = value;

            FirePropertyChangedEvent("Name");

        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyChangedEvent(string propertyName)
    {

        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

}

The people Collection is filled with Person objects. They are created in the following function... listValues is my ListBox.

void svc_GetHierachyCompleted(object sender, HCMobileSvc.GetHierachyCompletedEventArgs e)
    {
        var data = e.Result.ToArray();
        listValues.ItemsSource = null;
        people.Clear();

        int i = 0;
        foreach(var item in data)
        {
            if (i == 0)
            {
                // Manager
                mgrField1.Text = item[1].ToString();
                mgrField2.Text = item[2].ToString();
                i++;
            }
            else
            {
                // Untergebenen hinzufügen
                people.Add(new Person(item[0].ToString(), item[1].ToString(), item[2].ToString()));
            }

        }

        // Update List
        listValues.ItemsSource = people;

    }

Now I have a DataTemplate with two textblocks bound to both properties Id and Name. When the SelectionChanged event is fired I try to rebuild the entire list (so I call the function above again) using the following code:

            string id = people[listValues.SelectedIndex].Id;
        MessageBox.Show(id);
        CreateHierachy(id);

The CreateHierachy just only queries a WebService which then goes into the method above. The problem is, as soon as I select a value in the ListBox I get the following error:

ArgumentOutOfRangeException {"\r\nParameter name: index"}

The error is caused by the line listValues.SelectedIndex. I absolutely have no idea why that happens. What I know is that the MessageBox shows me the correct SelectedIndex value. What I also know is that when I remove the line people.Clear() that the error goes away but the ListBox does not get Updated.

Any ideas where the problem might be?

Thanks!!!

Bye, WorldSignia

You should check here for SelectedIndex being >= 0:

if (listValues.SelectedIndex >= 0)
     string id = people[listValues.SelectedIndex].Id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM