I have an MVVM Light ViewModel, which has an item inside:
public ObservableCollection<ObservableKeyValuePair<string, string>> OtherParticipants { get; set; }
The item itself is pretty straightforward
[ImplementPropertyChanged]
public class ObservableKeyValuePair<TKey, TValue>
{
public TKey Key { get; set; }
public TValue Value { get; set; }
}
I'm trying to bind it to my Xamarin.Android ListView wwith something like this:
var vm = VmLocator.Chat;
vm.InitClient(client);
vm.InitModel();
var contactsList = FindViewById<ListView>(Resource.Id.lvContacts);
vm.SetBinding(() => vm.OtherParticipants, contactsList.Adapter, BindingMode.TwoWay);
The problem is that this line highlights as syntactically incorrect and I am absolutely sure that contactsList.Adapter
is not the way I can bind my collection to it, but what is the correct way? In addition, how can I define the display member as I can do in WPF. Something like:
<ListBox SelectionMode="Single" ItemsSource="{Binding OtherParticipants}" SelectedItem="{Binding SelectedParticipant}" DisplayMemberPath="Value"/>
I was able to find a working answer for this problem.
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;
}
The valuable part of this are the last 2 lines of OnCreate
method. I've found the answer here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.