In this situation, I have to add an additional property to the model that combines the two properties.
public class ContactInfo { public string FirstName { get; set; } public string LastName { get; set; } public string FirstLastName { get { return FirstName + " " + LastName; }}
Now in your ViewModel, if the first or last name is changed, you will need to do something like this to update the FirstLastName property:
private string _firstLastName; public string FirstLastName { get { return _firstLastName; } set { if(_firstLastName != value) { _firstLastName = value; SetPropertyChanged(); } } } private string _firstName; public string FirstName { get { return _firstName; } set { if(_firstName != value) { _firstName = value; SetPropertyChanged(); SetPropertyChanged("FirstLastName");
Then do the same for you LastName .
Edit: your XAML will look like this:
<StackLayout Padding="20,0,0,0" HorizontalOptions="CenterAndExpand" > <Label x:Name="FirstName" Text="{Binding FirstLastName}"/> ..... </StackLayout>
Edit2: Since you probably never change the First or Last Name property when displaying the user interface, you just need to add the property to your model as shown in the ContactInfo code above, and then change the shortcut as shown in the edit above. and you will be good to go.
source share