【发布时间】:2017-08-18 01:13:47
【问题描述】:
假设我有一个只读模型...
[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>
还有一个 ViewModel(基本思想)
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 绑定到一个 ObservableCollection 类型:Person。当用户键入要搜索的名称时,它会从 ServiceStack 调用中填充。
目前 DataTemplate 中的 Label 绑定到 FirstName,但我希望它是一个新属性:FullName (Person.FirstName+" "+Person.LastName)。
如何将属性添加到无法编辑的模型中?我是否需要一个单独的 VM 用于模型本身并将 ObservableCollection 更改为该类型?任何例子都会很棒!
对不起,如果这是一个基本问题,我对 Xamarin 还很陌生。
谢谢!
【问题讨论】:
-
更改您的数据模板