windows-phone-7silverlight-toolkitlistpicker

ListPicker and "SelectedItem must always be set to a valid value"


I have a class in WCF:

[DataContract]
public class Usuario
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string Nombre { get; set; }

    [DataMember]
    public string Contraseña { get; set; }
}

In WP7 Proyect read ObservableCollection from WCF and load a ListPicker with:

lpUsuarios.ItemSource = listUsuarios;

This work ok.

Now, in WP7 use "Usuario _usuario = new Usuario()" for local variable.

The problem is, if I save the variable _usuario with IsolatedStorage and then load and apply in: lpUsuarios.SelectedItem = _usuario, causes the error: SelectedItem must always be set to a valid value.

Example:

Usuarios _usuario = new Usuario();

private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
     var settings = IsolatedStorageSettings.ApplicationSettings;
     _usuario = lpUsuarios.SelectedItem as Usuario;

     settings.Add("test", _usuario);
     settings.Save();
}

Now, close the application and then open:

private void ButtonLoad_Click(object sender, RoutedEventArgs e)
{
     settings.TryGetValue<Usuario>("test", out _usuario);            
     lpUsuarios.SelectedItem = _usuario;  <--- ERROR SelectedItem must....
}

In vs2010 debug, when open the application and load the variable _usuario, value is correct, but not work.

Error in: SelectedItem must always be set to a valid value, in ListPicker.cs

Location in ListPicker.cs:

        // Synchronize SelectedIndex property
        if (!_updatingSelection)
        {
            _updatingSelection = true;
            SelectedIndex = newValueIndex;
            _updatingSelection = false;
        }

Any solution?


If I use SelectedIndex, work ok, thanks Etch.

But now, the problem is if I want use:

    public override bool Equals(object obj)
    {
        return ID == (obj as Users).ID;
    }

    public override int GetHashCode()
    {
        throw new NotImplementedException();
    }

Where implement that, ¿in WCF class, in ModelView?

In XAML use:

    SelectedItem={Binding SelectedUser, Mode=TwoWay}"

And in ModelView use:

    private Usuario selectedUser;
    public Usuario SelectedUser
    {
        get
        {
            return selectedUser;
        } //----------------if i use modelview, the error is produced here
        set
        {
            selectedUser= value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedUser"));
            }
        }
    }

WCF class:

   [DataContract]
   public class Usuario
   {
       [DataMember]
       public int ID { get; set; }

       [DataMember]
       public string Nombre { get; set; }

       [DataMember]
       public string Contraseña { get; set; }
   }

Solution

  • Your collection doesn't have item that you want to select. Even if looks the same, smell the same, but it's a different object. Your Users class must override Equals method for this:

        public class Users
        {
            public int ID { get; set; }
            public string Nombre { get; set; }
    
            public override bool Equals(object obj)
            {
                return ID == (obj as Users).ID;
            }
    
            public override int GetHashCode()
            {
                throw new NotImplementedException();
            }
        }