【发布时间】:2016-04-12 13:47:48
【问题描述】:
我有一个带有自定义类列表的视图模型。此列表使用 MvxListView 显示在我的 ui 中。我可以轻松访问 custom 类的属性,但我现在需要的是一种从项目模板访问视图模型的方法修改类:
班级:
public class MyDataClass
{
public string Name {get; set;};
}
视图模型:
public class MyViewModel : MvxViewModel
{
//...
private string _sample;
public string Sample
{
get { return this._sample; }
private set { this.SetProperty(ref this._sample, value); }
}
private List<MyDataClass> _dataList;
public List<MyDataClass> DataList
{
get { return this._dataList; }
private set { this.SetProperty(ref this._dataList, value); }
}
}
视图View里面的MvxListView:
<Mvx.MvxListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/testListView"
local:MvxBind="ItemsSource DataList"
local:MvxItemTemplate="@layout/data_item" />
最后,data_item:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceLarge"
local:MvxBind="Text Name + ???.Sample"
android:textColor="@color/primary"
android:gravity="right|center_vertical"
android:paddingEnd="5dp" />
我需要得到的是???.Sample,它应该是 MyViewModel.Sample - 但我不知道如何得到它!
注意: 我不能修改 MyDataClass 来添加 ViewModel 作为属性,这个必须保持不变。 Viewmodel 本身和视图可以毫无问题地修改,以解决这个问题。
【问题讨论】:
-
可以修改 MyViewModel 类吗?
-
可以,只是 MyDataClass 必须保持不变
标签: c# android listview xamarin mvvmcross