ksumarine
ksumarine

Reputation: 782

Xamarin MVVM Custom Model Properties?

Say I have a readonly Model...

[DataContract]
public partial class Person {
    [DataMember]
    public virtual string LastName { get; set; }

    [DataMember]
    public virtual string FirstName { get; set; }
}

A View...

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="TheApp.APage"
    BackgroundColor="#03264A"
    Padding="0,0,0,0">
    <ContentPage.Content>
        <StackLayout
            BackgroundColor="Transparent">
            <ListView 
                ItemsSource="{Binding PersonSearchCollection}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label
                                Text="{Binding FirstName}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

And a ViewModel (basic idea)

namespace TheApp.ViewModels {
    public class APagePageViewModel : Helpers.BindableBase {

        private ObservableCollection<Person> _PersonSearchCollection = new RangeObservableCollection<Person>();

        public ObservableCollection<Person> PersonSearchCollection {
            get { return _PersonSearchCollection; }
            set { SetProperty(ref _PersonSearchCollection, value); }
        }
    }
}

My ListView is bound to an ObservableCollection of type: Person. This gets filled from a ServiceStack call when the user types a name to search.

Currently the Label in the DataTemplate is bound to FirstName, but I would like it to be a new property: FullName (Person.FirstName+" "+Person.LastName).

How would I add the property to the Model I cannot edit? Do I need a separate VM for the model itself and change the ObservableCollection to be of that type instead? Any examples would be great!

Sorry if this is a basic question, I'm fairly new to Xamarin.

Thank you!

Upvotes: 1

Views: 308

Answers (1)

Ramankingdom
Ramankingdom

Reputation: 1486

Change your view cell

<ViewCell>
 <StackLayout Orientation="Horizontal">
  <Label Text="{Binding FirstName}"
  <Label Text=" "
  <Label Text="{Binding LastName}"
 </StackLayout>
</ViewCell>

Other way

  1. Define One Custom Object

    public class CustomPerson
        {
            public CustomPerson(Person P)
            {
                FirstName = P.FirstName;
                LastName = P.LastName;
            }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string FullName
            {
                get { return string.Format("{0} {1}", FirstName, LastName);}
            }
    
        }
    

(2) Define One Collection with Getter

public IEnumerable<CustomPerson> CustomCollection
 {
  get { return _personSearchCollection.Select(p => new CustomPerson(p)); }
 } 

When you are updating Person search collection raise property change for Custom Collection.

  1. Finally Binding with CustomCollection

Upvotes: 1

Related Questions