Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatePicker binding behavior unstable when used in conjunction with other controls. #260

Closed
StepKie opened this issue Mar 7, 2023 · 1 comment
Labels
bug Something isn't working control-datepickerfield
Milestone

Comments

@StepKie
Copy link
Contributor

StepKie commented Mar 7, 2023

Sample repro can be observed here: https://github.com/Hottemax/MauiPlayground
launch app and go to DateAndTimeTestPage

The below code will lead to the following weird behaviors:

  • Full DateTime string on TextField will be set only after two clicks (first the Date portion, then the Time portion)
  • Setting to a specific value is ok, as opposed to
  • binding to DateTime.Now, which leads to an infinite loop

image

https://github.com/Hottemax/MauiPlayground/blob/master/MauiPlayground/MauiPlayground/Pages/DateAndTimeTestPage.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<uranium:UraniumContentPage
  x:Class="MauiPlayground.Pages.DateAndTimeTestPage"
  xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  xmlns:ctrls="clr-namespace:MauiPlayground.Controls"
  xmlns:local="clr-namespace:MauiPlayground.Pages"
  xmlns:material="clr-namespace:UraniumUI.Material.Controls;assembly=UraniumUI.Material"
  xmlns:uranium="clr-namespace:UraniumUI.Pages;assembly=UraniumUI"
  xmlns:vm="clr-namespace:MauiPlayground.ViewModels"
  x:DataType="vm:DateAndTimeViewModel">
  <StackLayout Spacing="20">
    <Label Text="SampleDate" />
    <material:TextField Title="BoundValue" Text="{Binding SampleDate}" />
    <Button Command="{Binding SetToNullCommand}" Text="SetNull" />
    <Button Command="{Binding SetToNewYearCommand}" Text="SetTo1-1" />
    <Button Command="{Binding SetToNewYear2Command}" Text="SetTo1-2" />
    <!--  BUG! Using DateTime.Now will lead to an infinite loop, setting another Date works fine  -->
    <Button Command="{Binding SetToNowCommand}" Text="SetToNow" />
    <!--  Leaving this in as a cautionary tale; see summary comment in DateAndTimePicker.xaml.cs  -->
    <material:DatePickerField Title="MatDate" Date="{Binding SampleDate}" />
    <!--<ctrls:DateAndTimePickerView DateAndTime="{Binding SampleDate}" LabelTextDate="{Binding TestStringDate}" LabelTextTime="{Binding TestStringTime}" />-->
  </StackLayout>
</uranium:UraniumContentPage>

where the viewmodel is simply https://github.com/Hottemax/MauiPlayground/blob/master/MauiPlayground/MauiPlayground/ViewModels/DateAndTimeViewModel.cs

https://github.com/Hottemax/MauiPlayground/blob/master/MauiPlayground/MauiPlayground/ViewModels/DateAndTimeViewModel.namespace MauiPlayground.ViewModels;

public partial class DateAndTimeViewModel : ObservableObject
{
    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(TimeOfSampleDate))]
    public DateTime? _sampleDate = DateTime.Now;

    public TimeSpan? TimeOfSampleDate => SampleDate.HasValue ? SampleDate.Value.TimeOfDay : null;

    [ObservableProperty]
    public TimeSpan? _sampleTime = new(hours: 3, minutes: 14, seconds: 0);

    [ObservableProperty]
    public string _testStringDate = "TestDate";

    [ObservableProperty]
    public string _testStringTime = "TestTime";

    public DateAndTimeViewModel()
    {
    }
    partial void OnSampleDateChanged(DateTime? value)
    {
        Log.Debug($"SampleDate changed to {value}");
    }

    [RelayCommand]
    public void SetToNull() => SampleDate = null;

    [RelayCommand]
    public void SetToNewYear() => SampleDate = new(year: 2023, month: 1, day: 1, hour: 1, minute: 2, second: 0);

    [RelayCommand]
    public void SetToNewYear2() => SampleDate = new(year: 2023, month: 1, day: 2, hour: 1, minute: 2, second: 0);

    [RelayCommand]
    public void SetToNow() => SampleDate = DateTime.Now;
}
@enisn enisn added the bug Something isn't working label Mar 7, 2023
@enisn enisn added this to the 2.3 milestone Mar 7, 2023
@enisn enisn modified the milestones: 2.3, Backlog May 11, 2023
@StepKie
Copy link
Contributor Author

StepKie commented Jan 15, 2024

Closing this in favor of getting #84 fixed.
The behavior here is more attributable to binding two controls to the same property, and them interacting in unforeseen ways through PropertyChanged events.

@StepKie StepKie closed this as completed Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working control-datepickerfield
Projects
None yet
Development

No branches or pull requests

2 participants