简体   繁体   English

Wp7 ListBox ItemSource ObservableCollection IndexOUTofRange 和 Items 未更新

[英]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.我有以下问题:我正在创建一个 Windows Phone 7 应用程序,并且我正在使用一个绑定到 ObservableCollection 人的 ListBox。 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. people 集合中充满了 Person 对象。 They are created in the following function... listValues is my ListBox.它们是在以下 function 中创建的... listValues 是我的 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.现在我有一个 DataTemplate,其中有两个文本块绑定到属性 Id 和 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:当 SelectionChanged 事件被触发时,我尝试使用以下代码重建整个列表(因此我再次调用上面的 function):

            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. CreateHierachy 只查询一个 WebService,然后进入上面的方法。 The problem is, as soon as I select a value in the ListBox I get the following error:问题是,只要我 select 在 ListBox 中有一个值,我就会收到以下错误:

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

The error is caused by the line listValues.SelectedIndex.该错误是由行 listValues.SelectedIndex 引起的。 I absolutely have no idea why that happens.我完全不知道为什么会这样。 What I know is that the MessageBox shows me the correct SelectedIndex value.我所知道的是 MessageBox 向我显示了正确的 SelectedIndex 值。 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.我还知道,当我删除 people.Clear() 行时,错误消失但 ListBox 没有得到更新。

Any ideas where the problem might be?问题可能出在哪里?

Thanks!!!谢谢!!!

Bye, WorldSignia再见,世嘉

You should check here for SelectedIndex being >= 0:您应该在此处检查SelectedIndex是否 >= 0:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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