DatePicker Binding Not Binding

CSharperCSharper Member ✭✭
edited January 18 in Xamarin.Forms

Hi, So I'm still new in Xamarin Forms and has been asked to develop an app wherein it keeps track of schedules of the business. Such as when the business started, the business ended and so on.

But before I explain what's happening here's the code for the view

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="xamarin.com/schemas/2014/forms"
             xmlns:x="schemas.microsoft.com/winfx/2009/xaml"
             x:Class=""
             xmlns:local=""
             Title="Lock" Icon="">
    <ContentPage.Content>
        <StackLayout>
            <StackLayout Padding="10">
                <TableView Intent="Data">
                    <TableRoot>
                        <TableSection Title="Schedule">
                            <ViewCell>
                                <StackLayout Orientation="Horizontal" Padding="20,0">
                                    <Label Text="Sched Start" HorizontalOptions="StartAndExpand" VerticalOptions="Center" FontSize="Small"></Label>
                                    <DatePicker Format="MM/dd/yyyy" Date="{Binding SchedStartDate, Mode=TwoWay}" HorizontalOptions="EndAndExpand"></DatePicker>
                                    <TimePicker HorizontalOptions="EndAndExpand"></TimePicker>
                                </StackLayout>
                            </ViewCell>

                            <ViewCell>
                                <StackLayout Orientation="Horizontal" Padding="20,0">
                                    <DatePicker Format="MM/dd/yyyy" Date="{Binding SchedEndDate, Mode=TwoWay}" HorizontalOptions="EndAndExpand"></DatePicker>
                                    <TimePicker HorizontalOptions="EndAndExpand"></TimePicker>
                                </StackLayout>
                            </ViewCell>
                        </TableSection>
                    </TableRoot>
                </TableView>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

This one is the code for the ViewModel

    public DateTime? SchedStartDate
            {
                get
                {
                    return this.schedStartDate;
                }

                set
                {
                    if (value != this.schedStartDate)
                    {
                        this.schedStartDate = value;
                        OnPropertyChanged();
                    }
                }
            }

            public DateTime? SchedEndDate
            {
                get
                {
                    return this.schedEndDate;
                }

                set
                {
                    if (value != this.schedEndDate)
                    {
                        this.schedEndDate = value;
                        OnPropertyChanged();
                    }
                }
            }

And I'm calling this in the code behind in OnAppearing() method.

        public async void GetLock (int ID)
                {
                    var result = await MyHttpRequest.GetSchedule(ID);

                    SchedStartDate = DateTime.Now;
                    SchedEndDate = DateTime.Now;
                    ActualStartDate = DateTime.Now;
                    ActualEndDate = DateTime.Now;
        }

So what's happening is whenever I run the app the first DatePicker, the one binded to "SchedStartDate" gets populated with the Data from the DB.
But the second one goes to the default.

Things I did:
1. I removed the first datepicker and the second datepicker gets the data from the DB.
2. 100% sure that the data is not default since I checked the data when I debugged.
3. Tried to use the same Binding Field for both DatePickers, but only the first one gets populated with the DB Data.
4. Tried to set the 2 variables to DateTime and it worked, but I would need to have null?

Note: This page is inside the TabbedPage, I doubt that's the issue, but just in case.

Hopefully someone can help me figure this out or did I miss something in the documentation, please let me know.
Any help is appreciated. :smiley:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Could you post the complete code especially that assigning value to the ViewModel ?

    I see SchedStartDate and SchedEndDate is set as DateYime.Now here.

  • CSharperCSharper Member ✭✭

    @ColeX said:
    Could you post the complete code especially that assigning value to the ViewModel ?

    I see SchedStartDate and SchedEndDate is set as DateYime.Now here.

    Thanks for replying @codex , So, I set it to DateTime.Now for simplicity's sake, but this will change to whatever the value in the DB is. Even with DateTime.Now is still goes to default.

    So, I did try something out. In the code behind I called the creation of the view model in the code behind constructor

    public partial class XXXX : ContentPage
        {
            int _lockID = 0;
            VM newVM;
    
            public XXXX(int lockID)
            {
                InitializeComponent ();
                _lockID = 145;
                newVM= new VM(_lockID);
    
            }
    
            protected override void OnAppearing()
            {
                try
                {
            //Before the creation of the VM is here...
                    BindingContext = newVM;
                }
    
                catch (Exception e)
                {
    
                }
    
                base.OnAppearing();
            }
        }
    

    Then the ViewModel's constructor is calling the GetLock (int ID).

    It did work, it is now being set to whatever value I choose it to be, but I was wondering why it is setting to default when I place the creation of the VM in OnAppearing()

  • ColeXColeX Member, Xamarin Team Xamurai

    Could you try to initialize the viewmodel in OnAppearing method instead of page constructor ?

     protected override void OnAppearing()
        {
            try
            {
        //Before the creation of the VM is here...
                _lockID = 145;
                newVM= new VM(_lockID);
                BindingContext = newVM;
            }
    
  • Sumit_SharmaSumit_Sharma USMember ✭✭✭
    edited January 25


    I have tried this and the above is working for me. But I am not using a database, could you please provide a sample of your project?

    And I would agree with @ColeX that you should initialize VM in OnAppearing instead of Constructor!

Sign In or Register to comment.