Refactor multiple if-else conditions when condition is a minor change I'm new programmer and I'm working on Xamarin MVVM app and I have a pin view like

So, basically  I have numbers from 0-9 if you pick one number its visible then if you pick a second one first one changed to * and I store all numbers into string called PinCode
ViewModel Code:
  public string PinCode { get; set; } = string.Empty;

        private async Task&lt;bool&gt; SelectedButton(Button button)
        {
            //If button is a number then
            if (button.Text != null)
            {
                if (PinCode.Length &lt; 4)
                {
                    PinCode = PinCode + button.Text;

                    //Assign every number field text depending of PinCode length
                    if (PinCode.Length == 1)
                    {
                        PinNumberOne = button.Text;
                    }
                    else if (PinCode.Length == 2)
                    {
                        PinNumberTwo = button.Text;
                        PinNumberOne = &quot;*&quot;;
                    }
                    else if (PinCode.Length == 3)
                    {
                        PinNumberThree = button.Text;
                        PinNumberOne = &quot;*&quot;;
                        PinNumberTwo = &quot;*&quot;;
                    }
                    else if (PinCode.Length == 4)
                    {
                        PinNumberFour = button.Text;
                        PinNumberOne = &quot;*&quot;;
                        PinNumberTwo = &quot;*&quot;;
                        PinNumberThree = &quot;*&quot;;
                    }
                }
            }
            // if it's backspace button then
            else
            {
                PinCode = PinCode.Remove(PinCode.Length - 1);

                if (PinCode.Length == 3)
                {
                    PinNumberFour = &quot;_&quot;;
                }
                else if (PinCode.Length == 2)
                {
                    PinNumberFour = &quot;_&quot;;
                    PinNumberThree = &quot;_&quot;;
                }
                else if (PinCode.Length == 1)
                {
                    PinNumberFour = &quot;_&quot;;
                    PinNumberThree = &quot;_&quot;;
                    PinNumberTwo = &quot;_&quot;;
                }
                else if (PinCode.Length == 0)
                {
                    PinNumberFour = &quot;_&quot;;
                    PinNumberThree = &quot;_&quot;;
                    PinNumberTwo = &quot;_&quot;;
                    PinNumberOne = &quot;_&quot;;
                }
            }
            return true;
        }

As you can see, I have a lot of repeated code and I know it's possible to improve this method much better, but I can not find the way. If you guys have an idea of a recursion or how can I refactor this code to do something much clear and clean I really appreciate it. Regards
This is a common problem. Sometimes it's tricky to generalise a process even when its iterative in nature.
By keeping track of both:

the location of the user's cursor (cursor_position), and
an iterable data structure storing which characters are shown (pinNumbers),

it is possible to implement this behaviour without so much repeated code. This gave the desired behaviour after some thorough testing (in my imagination).
public string PinCode { get; set; } = string.Empty;

private int cursor_position = -1;
private string[] pinNumbers = new string[] {&quot;_&quot;, &quot;_&quot;, &quot;_&quot;, &quot;_&quot;};

    private async Task&lt;bool&gt; SelectedButton(Button button)
    {
        //If button is a number then
        if (button.Text != null)
        {
            if ( cursor_position &lt; 3 )
            {
                PinCode = PinCode + button.Text;

                if ( cursor_position &gt; 0 )
                {
                    pinNumbers[cursor_position] = &quot;*&quot;;
                }

                cursor_position += 1;
                pinNumbers[cursor_position] = button.Text;
            }
        } else { // apparently backspace?
            if ( cursor_position &gt;= 0 )
            {
                PinCode = PinCode.Remove(PinCode.Length - 1);
                pin_numbers[cursor_position] = &quot;_&quot;;
                cursor_position -= 1;
            }
        }

        pinNumberOne   = pinNumbers[0]; // update actual shown values with array
        pinNumberTwo   = pinNumbers[1]; // you could probably take out the &quot;pinNumberX&quot; middleman
        pinNumberThree = pinNumbers[2]; // but I dont know what relation these variables have to
        pinNumberFour  = pinNumbers[3]; // the rest of your code

        return true;
    }

Depending on how you are assigning values to the actual rendered characters you may be able to remove the first four of the last five lines. You likely could just refer the rendered components directly to the values in the pinNumbers array.