【问题标题】:why dont show me the data ? WINRT为什么不给我看数据? WINRT
【发布时间】:2013-08-03 03:07:21
【问题描述】:

我尝试做一个简单的练习,但是,当我测试第一部分(显示人员数据)时,我没有在 UI 中显示任何内容。

我检查了绑定,我想也可以,我在按钮的单击事件中进行调试并获取所有数据,但 UI 没有显示任何内容。

所有项目都在这里:Less_300kb 或:

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

二等

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

最后一课

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

}

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>

主页

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

        gvGroup.UpdateLayout();

}

【问题讨论】:

    标签: c# xaml windows-store-apps winrt-xaml windows-store


    【解决方案1】:

    好的,所以发生的事情是您的 GridView 正在获取 ListaPersonasAgrupada,然后您正在更改它,但您的 GridView 不知道它已更改。

    您需要让您的ViewModel 告诉您的GridView ListaPersonasAgrupada 已更改。您可以使用INotifyPropertyChanged 执行此操作。或者,如果您使用的是 MvvmLight,则可以使用 RaisePropertyChanged。

    另一种选择是重新设置 GridView 的 ItemsSource,但这会破坏绑定。

    【讨论】:

    • 是的!你说得对,在我的 ViewModel 类中实现了类继承 BindableBase(实现 InotifyPropertyChanged),并且我添加了 SetProperty 的 ListaPersonas 属性:private ObservableCollection&lt;Persona&gt; _ListaPersonas; public ObservableCollection&lt;Persona&gt; ListaPersonas { get { return _ListaPersonas; } set { SetProperty(ref _ListaPersonas, value); } } 完美运行! :) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 2019-05-10
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多