Wp7 ListBox ItemSource ObservableCollection IndexOUTofRange 和 Items 未更新

发布于 2024-11-17 06:31:21 字数 2400 浏览 1 评论 0原文

我有以下问题:我正在创建一个 Windows Phone 7 应用程序,并且正在使用一个绑定到 ObservableCollection 人员的 ListBox。您可以在下面看到这个的实现:

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

    }

}

people 集合充满了 Person 对象。它们是在以下函数中创建的...listValues 是我的列表框。

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;

    }

现在我有一个 DataTemplate,其中两个文本块绑定到属性 Id 和 Name。当 SelectionChanged 事件被触发时,我尝试使用以下代码重建整个列表(因此我再次调用上面的函数):

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

CreateHierachy 仅查询 WebService,然后该 WebService 进入上面的方法。问题是,一旦我在 ListBox 中选择一个值,就会收到以下错误:

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

该错误是由 listValues.SelectedIndex 行引起的。 我完全不知道为什么会发生这种情况。我所知道的是 MessageBox 显示了正确的 SelectedIndex 值。我还知道,当我删除 people.Clear() 行时,错误消失,但 ListBox 没有更新。

有什么想法可能是问题出在哪里吗?

谢谢!!!

再见, 世界西尼亚

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-11-24 06:31:21

您应该在此处检查 SelectedIndex 是否 >= 0:

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

You should check here for SelectedIndex being >= 0:

if (listValues.SelectedIndex >= 0)
     string id = people[listValues.SelectedIndex].Id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文