Solucionado (ver solução)
Solucionado
(ver solução)
5
respostas

Binding no datePicker não funciona

AULA 5 Meu problema é que o DatePicker não aparece valor de maneira alguma. Os valores passam pelas propriedades poré não são exibidas datas e hora

AgendamentoView.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="{Binding Veiculo.Nome}"
             x:Class="TestDrive.Views.AgendamentoView">

    <StackLayout>
        <TableView>
            <TableRoot>
                <TableSection Title="Seus Dados">
                    <EntryCell Label="Nome" Text="{Binding Nome}"></EntryCell>
                    <EntryCell Label="Fone" Text="{Binding Fone}" Keyboard="Telephone"></EntryCell>
                    <EntryCell Label="E-Mail" Text="{Binding Email}" Keyboard="Email"></EntryCell>
                </TableSection>
                <TableSection Title="Agendamento">
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Margin="12">
                            <Label Text="Data" ></Label>
                            <DatePicker HorizontalOptions="End"
                                    Date="{Binding DataAgendamento}"></DatePicker>
                        </StackLayout>
                    </ViewCell>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Margin="12">
                            <Label Text="Hora"></Label>
                            <TimePicker Time="{Binding HoraAgendamento}" ></TimePicker>
                        </StackLayout>
                    </ViewCell>
                </TableSection>
            </TableRoot>
        </TableView>
        <Button Text="Agendar" Clicked="Button_Clicked">

        </Button>

    </StackLayout>

</ContentPage>

AgendamentoView.XAML.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestDrive.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class AgendamentoView : ContentPage
    {
        public veiculo Veiculo { get; set; }
        public string Nome { get; set; }
        public string Fone { get; set; }
        public string Email { get; set; }

        DateTime dataAgendamento = DateTime.Today;
        public DateTime DataAgendamento {
            get
            {
                return dataAgendamento;
            }
            set
            {
                dataAgendamento = value;
            }

        }
        public TimeSpan HoraAgendamento { get; set; }



        public AgendamentoView(veiculo veiculo)
        {
            InitializeComponent();
            Veiculo = veiculo;
            this.BindingContext = this;
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            DisplayAlert("Agendamento", "Nome" + Nome, "OK");
        }
    }
}
5 respostas

Olá.

Acredito que seja necessário setar o formato no DatePicker. Tenta colocar o atributo Format dentro, tipo assim:

<DatePicker Format="dd-MM-YYYY" ...>

Então Bruno, já tentei esta possibilidade também. Não funciona mesmo. Detalhe que utilizo o VS2017

solução!

Olá, Rafael!

Temos que tomar cuidado com a propriedade Margin:

Margin="12"

Todo o problema está aqui. Por quê? Porque Margin="12" significa que a margem do controle StackLayout terá margem 12. Não só a margem esquerda e direita, mas também a margem superior e a margem inferior. O problema é que o StackLayout está contido num controle ViewCell, que tem altura limitada. E nessa altura limitada, você adicionou uma margem superior e outra inferior de 12, o que reduziu a altura do StackLayout em 24 unidades.

O que você pode fazer é adicionar margem somente onde interessa, ou seja, à esquerda e à direita:

Margin="[esquerda],[superior],[direita],[inferior]"

Ou melhor:

Margin="12,0,12,0"

Boa sorte e bons estudos!

Obrigado Marcelo era isso mesmo. Curso excelente.

Excelente, Rafael! E obrigado por nos prestigiar!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software