Listview Databind Xamarin Forms

Asked

Viewed 208 times

0

I’m creating an app with Xamarin Forms. The app has a student profile screen where I need to display the Model phone list. It turns out that when I do listview Binding, all the other form bindings stop working. Does anyone have any idea what it might be?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="GeoMobile.Views.DadosCadastrais">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"></ColumnDefinition>
      <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Label Text="Foto" Grid.Row="0" Grid.Column="0" />
    <StackLayout  Grid.Row="0" Grid.Column="1">
      <Label Text="{Binding Aluno.Nome}" />
      <Label Text="{Binding Aluno.DataNascimento, StringFormat='{0:dd/MM/yyyy}'}" />
      <Label Text="{Binding Aluno.RG, StringFormat='RG: {0}'}" />
      <Label Text="{Binding Aluno.CPF, StringFormat='CPF: {0}'}" />
    </StackLayout>
    <Label Text="Contato" Grid.Row="1" Grid.Column="0" BackgroundColor="Gray" Grid.ColumnSpan="2"></Label>
    <Label Text="{Binding Aluno.Email}" Grid.Row="2" Grid.Column="0"></Label>
    <ListView ItemsSource="{Binding Aluno.Telefones}"  Grid.Row="3" Grid.Column="0">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout Orientation="Horizontal">
              <Label Text="{Binding Tipo, StringFormat='{0}: '}"></Label>
              <Label Text="{Binding Telefone}"></Label>
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
    <Label Text="Endereço Residencial" Grid.Row="4" Grid.Column="0" BackgroundColor="Gray" Grid.ColumnSpan="2"></Label>
    <StackLayout Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2">
      <Label Text="{Binding Aluno.EnderecoResidencial.Logradouro}" />
    </StackLayout>
  </Grid>
</ContentPage>

This is the View Model:

public class DadosCadastraisVM : Model.ObservableBase
    {
        private AlunoModel _aluno;

        public AlunoModel Aluno
        {
            get { return _aluno; }
            set
            {
                _aluno = value;
                OnPropertyChanged();
            }
        }

        public DadosCadastraisVM()
        {
            Aluno = new AlunoModel
            {
                Nome = "Aluno Teste",
                DataNascimento = new DateTime(2000, 08, 04),
                RG = "23.588.555-45",
                CPF = "111.222.333-44",
                Email = "[email protected]",
                Telefones = new ObservableCollection<TelefoneModel>
                {
                    new TelefoneModel {Tipo = "Celular", Telefone = "(11) 99009-8899" },
                    new TelefoneModel { Tipo = "Residencial", Telefone = "(11) 4499-2233" }
                },
                EnderecoComercial = new Endereco
                {
                    Logradouro = "Rua teste",
                    Numero = "2937",
                    Bairro = "Bairro teste",
                    Cidade = "São paulo",
                    Estado = "SP",
                    Cep = "02233-223",
                    Complemento = string.Empty
                },
                EnderecoResidencial = new Endereco
                {
                    Logradouro = "Rua teste",
                    Numero = "2937",
                    Bairro = "Bairro teste",
                    Cidade = "São paulo",
                    Estado = "SP",
                    Cep = "02233-223",
                    Complemento = string.Empty                    }
            };
        }
    }

This is the Student class with the Address and Telephone classes:

public class AlunoModel
    {
        public string Nome { get; set; }
        public DateTime DataNascimento { get; set; }
        public string RG { get; set; }
        public string CPF { get; set; }
        public string Email { get; set; }
        public ObservableCollection<TelefoneModel> Telefones { get; set; }
        public Endereco EnderecoComercial { get; set; }
        public Endereco EnderecoResidencial { get; set; }
    }

public class TelefoneModel
    {
        public string Tipo { get; set; }
        public string Telefone { get; set; }
    }

public class Endereco
    {
        public string Logradouro { get; set; }
        public string Numero { get; set; }
        public string Bairro { get; set; }
        public string Cep { get; set; }
        public string Cidade { get; set; }
        public string Estado { get; set; }
        public string Complemento { get; set; }
    }
  • Try: <Label Text="{Binding Type, Stringformat='{0:}'}"/>

  • I tried that way Victor, but it wasn’t. The same thing happens....

  • Hello Samuel. .

  • You can check in the output window. Generally, are logged information about Bindings!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.