locked
Text value doesn't display until Entry control is clicked RRS feed

  • Question

  • User392993 posted

    A TabbedPage contains multiple ContentPages. A few controls on one of these ContentPages are databound to an object property called SelectedCourse in MyPageModel. MyPageModel implements INotifyPropertyChanged and is assigned to the BindingContext of the ContentPage.

              <ContentPage.BindingContext>
                    <local:MyPageModel/>
              </ContentPage.BindingContext>
    

    When the ContentPage opens it should display the course details that are retrieved by the constructor of MyPageModel.

        public MyPageModel()
        {
            GetCurrentCourseAsync();            
        }
    
        private Course selectedCourse;
        public Course SelectedCourse
        {
            get => selectedCourse;
            set
            {
                selectedCourse = value;
                NotifyPropertyChanged("SelectedCourse");
            }
        }
    
        private async void GetCurrentCourseAsync()
        {
            var course = await courseRepo.GetCourseAsync();
        SelectedCourse = course;
        }
    

    The data-bound values for the Picker and DatePicker controls display fine, but none of the Entry controls display a text value until I physically click on the control itself, and then the data appears in the control. I'm not sure why this is happening or what I need to do to refresh the UI so that these data-bound Entry controls display their data when the ContentPage is first opened?

    Wednesday, July 8, 2020 10:05 PM

Answers

  • User392993 posted

    So I changed the Entry controls to Labels and everything started working as expected, with data appearing when the page loads and no need to click on the controls themselves to see it.

    I ended up adding a Placeholder property to each of the Entry controls, and now the data appears as expected when the page is loaded. I can't explain it, but it seems to have resolved the issue.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 9, 2020 9:55 PM

All replies

  • User369979 posted

    The SelectedCourse property here is a type of Course. How did you bind it to your Entry? And I noticed that you called an async method in the constructor. This could also be a potential issue. How about hard coding your SelectedCourse in the constructor like:

    public MyPageModel()
    {
        SelectedCourse = new Course { id = 10, Name = "test", ... };
    }
    

    Moreover, could you please make a small sample and share it here to reveal this issue?

    Thursday, July 9, 2020 2:24 AM
  • User392993 posted

    I tried hard coding the SelectedCourse property in the constructor and the result was the same - Picker and DatePicker display the correct value, but none of the Entry controls display their value until I click on them individually. I changed the async method to a synchronous call but again the result was the same as above.

    SelectedCourse is set to the Course entity returned by the async (or synchronous) method in the constructor, and SelectedCourse is also bound to the ContentPage controls as follows:

    <Picker SelectedItem="{Binding Path=SelectedCourse.CourseName, Mode=TwoWay}">
      <Picker.Items>
        <x:String>Course A</x:String>
        <x:String>Course B</x:String>
        <x:String>Course C</x:String>>
      </Picker.Items>
    </Picker>
    <Entry Text="{Binding Path=SelectedCourse.InstructorName}"/>
    <Entry Text="{Binding Path=SelectedCourse.InstructorNumber}"/>
    <DatePicker Date="{Binding Path = SelectedCourse.StartDate}"/>
    <Entry Text="{Binding Path=SelectedCourse.Notes}"/>
    
    Thursday, July 9, 2020 4:03 AM
  • User369979 posted

    Could you please share your sample here to help me reproduce this issue?

    Thursday, July 9, 2020 5:11 AM
  • User392993 posted

    So I changed the Entry controls to Labels and everything started working as expected, with data appearing when the page loads and no need to click on the controls themselves to see it.

    I ended up adding a Placeholder property to each of the Entry controls, and now the data appears as expected when the page is loaded. I can't explain it, but it seems to have resolved the issue.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 9, 2020 9:55 PM