说我有一个只读的模式...Xamarin MVVM自定义模型属性?
[DataContract]
public partial class Person {
[DataMember]
public virtual string LastName { get; set; }
[DataMember]
public virtual string FirstName { get; set; }
}
的视角...
<?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>
而一个视图模型(基本思路)
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); }
}
}
}
我的ListView控件绑定到一个类型为Person的ObservableCollection。当用户输入要搜索的名称时,这会从ServiceStack调用中填充。
目前DataTemplate中的标签绑定到名字,但我希望它是一个新的属性:FullName(Person.FirstName +“”+ Person.LastName)。
我该如何将属性添加到我无法编辑的模型?我是否需要为模型本身使用单独的VM,并将ObservableCollection更改为该类型?任何例子都会很棒!
对不起,如果这是一个基本的问题,我对Xamarin相当陌生。
谢谢!
更改数据模板 <标签文本=“{结合姓} “ –
Ramankingdom