Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Text value doesn't display until Entry control is clicked

RcmpAduRcmpAdu Member ✭✭

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?

Best Answer

  • RcmpAduRcmpAdu Member ✭✭
    Accepted Answer

    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.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    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?

  • RcmpAduRcmpAdu Member ✭✭

    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}"/>
    
  • LandLuLandLu Member, Xamarin Team Xamurai

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

  • RcmpAduRcmpAdu Member ✭✭
    Accepted Answer

    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.

Sign In or Register to comment.