Question

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; }
   }
Was it helpful?

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

OTHER TIPS

You can't select an item that is not one of the items in the collection bound to ItemsSource.

I ran into this problem just the other day. Exact problem. There is a bug in the Listpicker. There was a link I have at home that goes into the details of it, but the simple work around is to do what you did and use the SelectedIndex property instead.

By the way I found another question on this exact topic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top