MAUI: Label.Text is not working when the label has PropertyChanged event

Sreejith Sreenivasan 1,001 Reputation points
2023-09-13T15:25:49.2133333+00:00

I am trying to set the label's Text value on the xaml.cs file which has PropertyChanged event. The text value is not showing on the UI with PropertyChanged event. If I remove the PropertyChanged event the text value is showing on the UI.

My Label:

	<Label 
		x:Name="selectcategory_label"
		PropertyChanged="selectcategory_label_PropertyChanged"
		Text="Select a category"
		Style="{StaticResource InvoiceUserLabelStyle}"/>

Settings text like below:

selectcategory_label.Text = "Books";

Is it a known issue?

Update

I am working on a form where I have a Title (Entry), Description (Editor), Category (Label), and a Submit Button. The Button is in disable state and after entering all the details it will be enable. That logic I have added on PropertyChanged event.

private void selectcategory_label_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (!string.IsNullOrWhiteSpace(title_entry.Text) && !string.IsNullOrWhiteSpace(description_editor.Text) && selectcategory_label.Text != "Select a category")
    {
        submit_button.IsEnabled = true;
        submit_button.BackgroundColor = Color.FromArgb("#f3872c");
    }
    else
    {
        submit_button.IsEnabled = false;
        submit_button.BackgroundColor = Color.FromArgb("#d5d5d5");
    }
}

When setting category I will check the title and descriptions are available and based on that I am enabling the button. That's all I am doing on PropertyChanged event.

I commented out the code in selectcategory_label_PropertyChanged event and label is working at that time and I am working in debug mode.

Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Anonymous
    2023-09-14T09:09:17.1166667+00:00

    Hello,

    I commented out the code in selectcategory_label_PropertyChanged event and label is working fine at that time.

    You can move this PropertyChanged="selectcategory_label_PropertyChanged" for label.

    Then move this part code to a new method like following code.

    public void CheckInPutValue()
    {
         if (!string.IsNullOrWhiteSpace(title_entry.Text) && !string.IsNullOrWhiteSpace(description_editor.Text) && selectcategory_label.Text != "Select a category")
         {
             submit_button.IsEnabled = true;
             submit_button.BackgroundColor = Color.FromArgb("#f3872c");
         }
         else
         {
             submit_button.IsEnabled = false;
             submit_button.BackgroundColor = Color.FromArgb("#d5d5d5");
         }
    }
    

    Then add same textchange event for your Title (Entry), Description (Editor) like following code.

    <Entry x:Name="title_entry" Text="" TextChanged="title_entry_TextChanged"></Entry>
    <Editor x:Name="description_editor" Text="" TextChanged="title_entry_TextChanged"></Editor>
    

    Next, check the input value in textchange event.

    private void title_entry_TextChanged(object sender, TextChangedEventArgs e)
    {
       
         CheckInPutValue();
    }
    

    As note, please add CheckInPutValue method in the OnAppearing() method.

    protected async override void OnAppearing()
    {
         CheckInPutValue();
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.