2
votes

I am a little confused as to how best to display 1 of 3 different strings based on a condition. I have 2 data fields - mobile number and telephone number and I want to display them in a listview. If there is a mobile number I want it to display that otherwise if there is no mobile number but a telephone number I want to display the telephone number. Or if there is neither a mobile number or telephone number then I want to display the string "No number available".

< Label Text="{Binding NumberText}" TextColor="Teal" FontSize="11"/>

2
That logic should be done in the ViewModel. And only one String will be used to be bounded to the label, the content of that string you can change in the ViewModel/Code behindGreggz
Another way would be to bind the whole model and write a converter that takes the model, gets the properties and does the check.Johannes

2 Answers

3
votes

Use a public property that returns the desired string based on your conditions (consider using a ViewModel)

public string NumberText
{
    get
    {
        if (this.hasMobileNumber)
        {
            return this.mobileNumber;
        }

        if (this.hasPhoneNumber)
        {
            return this.phoneNumber;
        }

        return "No number available";
    }
}
0
votes

You can set this in your model

//Assuming phoneNumber and mobileNumber are declared in your model
[JsonIgnore]
    public string NumberText
    {
        get
        {
            string number = "";
            if (phoneNumber != null || phoneNumber != "")
            {
                number = phoneNumber;
            }

            else if (mobileNumber != null || mobileNumber != "")
            {
                number = mobileNumber;
            }

            else
            {
                number = "No number available";
            }
            return number;
        }
    }