ListPicker no marca los elementos seleccionados cuando ItemsSource está vinculado a los datos obtenidos del servicio

StackOverflow https://stackoverflow.com/questions/9336464

Pregunta

Seguí las instrucciones en http://mobileworld.appamundi.com/blogs/andywigley/archive/2012/02/02/how-to-databind-selecteditems-of-the-listpicker-and-recurringdayspicker. aspx para vincular SelectedItems en ListPicker de selección múltiple.

Cuando vinculo ItemsSource a ObservableCollection de objetos creados en ViewModel, ListPicker funciona sin ningún problema. Sin embargo, cuando vinculo ItemsSource a ObservableCollection obtenido del servicio WCF, comienzan los problemas. ListPicker muestra todos los elementos correctamente, también se pueden seleccionar y mostrar como seleccionados en la pantalla principal. Pero cuando hago clic en el selector para seleccionar nuevamente, ListPicker no puede "marcar" los elementos seleccionados en la plantilla.

Podría volver a escribir la lista de todos los elementos extraídos del servicio, pero me pregunto si hay una forma ordenada de resolver ese problema.

Información del proyecto: WP 7.0, servicios WCF, EntityFramework, mucho café

Clase de entidad:

DataContract(IsReference=true)]
public class TypeOfDish
{
    [DataMember]
    public int TypeOfDishID { get; set; }
    [DataMember]
    public string NameToDisplay { get; set; }
}

Servicio WCF:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [ApplyDataContractResolver]
    IEnumerable<TypeOfDish> GetDishTypes();
}

Xaml:

<StackPanel x:Name="DishTypeGroup" Orientation="Vertical" Width="400">
    <helpers:ListPickerExtended x:Name="TypeOfDishPicker" >
         ItemsSource="{Binding DishTypeList}" 
         SelectionMode="Multiple"
         SelectedItems="{Binding SelectedDishTypes, Mode=TwoWay}"
         Margin="8,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Center">

        <toolkit:ListPicker.FullModeItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=NameToDisplay}" />
            </DataTemplate>
        </toolkit:ListPicker.FullModeItemTemplate>
    </helpers:ListPickerExtended>
</StackPanel>

ListPickerExtended.cs

public class ListPickerExtended : ListPicker
{
    public new IList SelectedItems
    {
        get
        {
            return (IList)GetValue(SelectedItemsProperty);
        }
        set
        {
            base.SetValue(SelectedItemsProperty, value);
        }
    }
}

ViewModel

public ObservableCollection<TypeOfDish> DishTypeList
{
    get
    {
        //myModel.DichTypes is loaded with data from calling 
        //GetDishTypesAsync() on my service client
        return myModel.DishTypes;
    }
}

ObservableCollection<object> _selectedDishes = new ObservableCollection<object>();

public ObservableCollection<object> SelectedDishTypes
{
    get { return _selectedDishes; }
    set
    {
       if (_selectedDishes == value) { return; }
       _selectedDishes = value;
       RaisePropertyChanged("SelectedDishTypes");
    }
}
¿Fue útil?

Solución

Acabo de encontrar una solución.

Estaba obteniendo datos del servicio cada vez que se cargaba la página.Al regresar de la plantilla de modo completo en ListPicker, se volvió a cargar la página principal, restableciendo la propiedad DishTypesList utilizando datos recién recuperados.Aunque ListPicker.SelectedItems todavía estaba configurado, el nuevo objeto no coincidía con los objetos en ellos.

Solo necesitaba mover los datos de carga al constructor del modelo de vista y ListPicker comenzó a funcionar correctamente.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top