Search code examples
c#xamlwindows-store-appswinrt-xamlwindows-store

why dont show me the data ? WINRT


im try to do a simple excercise, but, when i test the first part (show person data) dont show me nothing into UI.

I checked the bindings and I guess are okay, too, I do a debug in the click event of the button and get all the data, but the UI does not show me anything.

all proyect here: Less_300kb or:

public class Persona: BindableBase
{
    private string _nombre;
    public string Nombre
    {
        get { return _nombre; }
        set { _nombre = value;
              SetProperty(ref _nombre, value);}
    }

    private string _apellido;
    public string Apellido
    {
        get { return _apellido; }
        set { SetProperty(ref _apellido, value); }
    }

    private int _cedula;
    public int Cedula
    {
        get { return _cedula; }
        set { _cedula = value;
              SetProperty(ref _cedula, value);}
    }

    private string _profesion;
    public string Profesion
    {
        get { return _profesion; }
        set { SetProperty(ref _profesion, value); }
    }
}

the second class

public class GrupoPersonas
{
    public string Profesion { get; set; }
    public List<Persona> ListaPersonas { get; set; }
}

the Last class

public class DataSourcePersonas
{
    //public List<Persona> ListaPersonas { get; set; }
    public ObservableCollection<Persona> ListaPersonas { get; set; }

    public void CrearLista()
    {
        var listaPivote = new ObservableCollection<Persona>() 
            {
            new Persona(){ Profesion="Ingeniero",Apellido="Ruiz Pacheco",Nombre="Juan Carlos"},
            new Persona(){ Profesion="Médico",   Apellido="Gonzalez Ramírez",     Nombre="Miguel"},
            new Persona(){ Profesion="Analista", Apellido="Ramirez",       Nombre="Angel"},
            new Persona(){ Profesion="Enfermero",Apellido="Aldana",        Nombre="Cesar"},
            new Persona(){ Profesion="Conductor",Apellido="Echeverry",    Nombre="Andres"},
            new Persona(){ Profesion="Piloto",   Apellido="Coronel",      Nombre="David"},
            new Persona(){ Profesion="Capitán",  Apellido="Baracaldo",         Nombre="Alejandro"},
            new Persona(){ Profesion="Biólogo",  Apellido="Palacios",      Nombre="Mauricio"},
            new Persona(){ Profesion="Físico",   Apellido="Botía",         Nombre="Oscar"},
            new Persona(){ Profesion="Astrónomo",Apellido="Heldford",     Nombre="Axwell"}
            };

        Random genCedula = new Random();
        var listaFull = from persona in listaPivote
                        from persona2 in listaPivote
                        from persona3 in listaPivote
                        select new Persona()
                        {
                            Cedula = (int)(genCedula.NextDouble() * 999999999),
                            Nombre = persona.Nombre,
                            Apellido = persona2.Apellido,
                            Profesion = persona3.Profesion
                        };

        //ListaPersonas = new List<Persona>(listaFull);
        ListaPersonas = new ObservableCollection<Persona>(listaFull);
    }

    public ObservableCollection<GrupoPersonas> ListaPersonasAgrupada { get; set; }
    public void CrearGrupo()
    {
        var lista = from persona in ListaPersonas
                    group persona by persona.Profesion into grupo
                    select new GrupoPersonas()
                    {
                        Profesion = grupo.Key,
                        ListaPersonas = grupo.ToList()
                    };
        ListaPersonasAgrupada = new ObservableCollection<GrupoPersonas>(lista);
    }

}

the xaml

<Page.Resources>
    <data:DataSourcePersonas x:Key="DataSourcePersonas"
                             x:Name="DataSourcePersonas"></data:DataSourcePersonas>
    <CollectionViewSource x:Key="CvsGruposPersonas" x:Name="CvsGruposPersonas" IsSourceGrouped="True"
                          Source="{Binding Source={StaticResource DataSourcePersonas}, Path=ListaPersonasAgrupada}"
                          ItemsPath="ListaPersonas"></CollectionViewSource>
</Page.Resources>


GridView x:Name="gvGroup" ItemsSource="{Binding Source={StaticResource DataSourcePersonas}, Path=ListaPersonas}" 
              Margin="10,113,10,10">
        <GridView.ItemTemplate>
            <DataTemplate>
                <StackPanel Style="{StaticResource apptile}">
                    <TextBlock Style="{StaticResource PersonName}"       Text="{Binding Nombre}"/>
                    <TextBlock Style="{StaticResource PersonName}"       Text="{Binding Apellido}"/>
                    <TextBlock Style="{StaticResource PersonCedula}"     Text="{Binding Cedula}"/>
                    <TextBlock Style="{StaticResource PersonProfession}" Text="{Binding Profesion}"/>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

MainPage

private void Button_Click(object sender, RoutedEventArgs e)
{
        DataSourcePersonas.CrearLista();
        DataSourcePersonas.CrearGrupo();

        gvGroup.UpdateLayout();

}

Solution

  • Okay, so what's happening is your GridView is getting the ListaPersonasAgrupada, and then you are changing it, but your GridView doesn't know that it was changed.

    You need to have your ViewModel tell your GridView that ListaPersonasAgrupada has changed. You can do this with INotifyPropertyChanged. Or, if you're using MvvmLight, you can use RaisePropertyChanged.

    The other option is to re-set the ItemsSource of the GridView, but that will break the binding.