c# - Xamarin MVVM Custom Model Properties? -
say have 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 viewmodel (basic idea)
namespace theapp.viewmodels { public class apagepageviewmodel : helpers.bindablebase { private observablecollection<person> _personsearchcollection = new rangeobservablecollection<person>(); public observablecollection<person> personsearchcollection { { return _personsearchcollection; } set { setproperty(ref _personsearchcollection, value); } } } }
my listview bound observablecollection of type: person. gets filled servicestack call when user types name search.
currently label in datatemplate bound firstname, new property: fullname (person.firstname+" "+person.lastname).
how add property model cannot edit? need separate vm model , change observablecollection of type instead? examples great!
sorry if basic question, i'm new xamarin.
thank you!
change view cell
<viewcell> <stacklayout orientation="horizontal"> <label text="{binding firstname}" <label text=" " <label text="{binding lastname}" </stacklayout> </viewcell>
other way
define 1 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 { { return string.format("{0} {1}", firstname, lastname);} } }
(2) define 1 collection getter
public ienumerable<customperson> customcollection { { return _personsearchcollection.select(p => new customperson(p)); } }
when updating person search collection raise property change custom collection.
- finally binding customcollection
Comments
Post a Comment