Rega hawlery as well as
How can I get the value of a TimePicker?
This screen consists of a form that will store the days of the week and their respective times when a sports court is open to the public.
The HoraInicio
(initial time) and HoraFim
(end time) properties are coming with the value 00: 00: 00
.
XAML
xaml
<StackLayout BindableLayout.ItemsSource="{Binding Horarios}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" HeightRequest="40">
<Switch x:Name="AbertoSwitch"
IsToggled="{Binding Aberto}"
Toggled="AlteracaoDoEstadoDoSwitch"
VerticalOptions="Center"/>
<Label Text="{Binding DiaDaSemanaText}"
HorizontalOptions="FillAndExpand"
VerticalOptions="Center"/>
<TimePicker BindingContext="{x:Reference AbertoSwitch}"
IsVisible="{Binding IsToggled}"
VerticalOptions="Center"
Format="HH:mm"
Time="{Binding HoraInicio}"/>
<TimePicker BindingContext="{x:Reference AbertoSwitch}"
IsVisible="{Binding IsToggled}"
VerticalOptions="Center"
Format="HH:mm"
Time="{Binding HoraFim}"/>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
``</StackLayout>
ViewModel
public TimeSpan HoraInicio { get => horaInicio; set => SetProperty(ref horaInicio, value); }
public TimeSpan HoraFim { get => horaFim; set => SetProperty(ref horaFim, value); }
public void AtribuirHorarioParaCadaDiaDisponivel()
{
try
{
for (int i = 0; i <= 6; i++)
{
if (this.Horarios[i].Aberto == true)
{
this.Horarios[i].HoraInicio = this.HoraInicio.ToString();
this.Horarios[i].HoraFim = this.HoraFim.ToString();
}
}
}
catch (Exception)
{
throw new Exception("Horário(s) incorreto(s)");
}
}
3 answers
Sort by: Newest
-
-
LandLu-MSFT 21 Reputation points
2020-11-19T03:45:54.137+00:00 Hello,
Welcome to Microsoft Q&A!
Do not change the binding context. We could use source to complete the binding:
<TimePicker IsVisible="{Binding IsToggled, Source={x:Reference HorarioSwitch}}" VerticalOptions="Center" Format="HH:mm" Time="{Binding BindingContext.HoraInicio, Source={x:Reference RootLayout}}"/>
So does another time picker:
<TimePicker IsVisible="{Binding IsToggled, Source={x:Reference HorarioSwitch}}" VerticalOptions="Center" Format="HH:mm" Time="{Binding BindingContext.HoraFim, Source={x:Reference RootLayout}}"/>
RootLayout
here is yourStackLayout
:
Name it as you want.
Thank you.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. -
Joe Manke 1,091 Reputation points
2020-11-18T20:46:28.933+00:00 You're binding the TimePicker to the Switch. Switch does not have HoraInicio or HoraFim properties.
Instead of this:
<TimePicker BindingContext="{x:Reference AbertoSwitch}" IsVisible="{Binding IsToggled}">
Just bind TimePicker.IsVisible to Aberto the same way you're binding Switch.IsToggled.