我有一个MVVM Light ViewModel,里面有一个项目:
public ObservableCollection<ObservableKeyValuePair<string, string>> OtherParticipants { get; set; }
该项目本身非常简单
[ImplementPropertyChanged]
public class ObservableKeyValuePair<TKey, TValue>
{
public TKey Key { get; set; }
public TValue Value { get; set; }
}
我正在尝试将它绑定到我的Xamarin.Android ListView,其中包含以下内容:
var vm = VmLocator.Chat;
vm.InitClient(client);
vm.InitModel();
var contactsList = FindViewById<ListView>(Resource.Id.lvContacts);
vm.SetBinding(() => vm.OtherParticipants, contactsList.Adapter, BindingMode.TwoWay);
问题是这一行突出显示语法不正确,我绝对相信contactsList.Adapter
不是我可以将我的集合绑定到它的方式,但是正确的方法是什么?另外,我如何在WPF中定义显示成员。类似的东西:
<ListBox SelectionMode="Single" ItemsSource="{Binding OtherParticipants}" SelectedItem="{Binding SelectedParticipant}" DisplayMemberPath="Value"/>
答案 0 :(得分:0)
我能找到解决这个问题的工作答案。
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MainChat);
var client = Nav.GetAndRemoveParameter<MyClient>(Intent);
var vm = VmLocator.Chat;
vm.InitClient(client);
vm.InitModel();
var contactsList = FindViewById<ListView>(Resource.Id.lvContacts);
contactsList.Adapter = vm.OtherParticipants.GetAdapter(ContactsListViewTemplate);
}
private View ContactsListViewTemplate(int position, ObservableKeyValuePair<string, string> participant,
View convertView)
{
var view = convertView ?? LayoutInflater.Inflate(Resource.Layout.ParticipantsListItem, null);
var firstName = view.FindViewById<TextView>(Resource.Id.tvParticipantName);
firstName.Text = participant.Value;
return view;
}
这有价值的部分是OnCreate
方法的最后两行。我找到了答案here