Listpicker error SelectedItem must always be set to a valid value

后端 未结 4 1056
-上瘾入骨i
-上瘾入骨i 2020-12-16 02:38

I have a page in a Windows Phone 7 app where the user can edit or delete an Transaction object. The Transaction object is an Linq-to-Sql class that have a relationship with

4条回答
  •  独厮守ぢ
    2020-12-16 02:56

    ListPicker uses Items.IndexOf to get the index of item instance that it should select.

    If the instance does not match (it is not an object instance from the collection) the IndexOf will return -1 and the InvalidOperationException is thrown with the message: "SelectedItem must always be set to a valid value".

    Override Equals method of the item type in the collection and it will work as expected.

    Example:

    public override bool Equals(object obj)
    {
             var target = obj as ThisType;
             if (target == null)
                 return false;
    
             if (this.ID == target.ID)
                 return true;
    
             return false;
     }
    

    Hope it helps

提交回复
热议问题