Hello,
Welcome to our Microsoft Q&A platform!
Do you want to achieve the following function?If yes, you can try the following steps:
To make the code structure clearer, we can create a new class for the picker (DataType
):
DataType.cs
public class DataType
{
public int Contact_ID { get; set; }
public int Contact_Type { get; set; }
public string Value { get; set; }
}
And a enum Type
for different types of contacts (Contact_Type
):
public enum Type:int
{
Contact_Address,
Contact_eMail,
Contact_Mobile,
}
FieldPicker.cs
public class FieldPicker
{
public static List<DataType> GetContacts()
{
var contacts = new List<DataType>()
{
new DataType(){ Contact_ID = 1, Contact_Type = (int)Type.Contact_Address ,Value = "Contact_Address"},
new DataType(){ Contact_ID = 1, Contact_Type = (int)Type.Contact_eMail ,Value = "Contact_eMail"},
new DataType(){ Contact_ID = 1, Contact_Type = (int)Type.Contact_Mobile ,Value = "Contact_Mobile"}
};
return contacts;
}
}
Contacts.cs
public class Contacts:BaseViewModel
{
public string Contact_Name { get; set; }
public string Contact_Address { get; set; }
public string Contact_eMail { get; set; }
public string Contact_Mobile { get; set; }
public string Contact_DOB { get; set; }
public string Contact_Designation { get; set; }
public string Contact_JoiningDate { get; set; }
public string Contact_MaritalStatus { get; set; }
public string Contact_Method { get; set; }
public string Value { get; set; }
public int Key { get; set; }
private string _contactText;
public string ContactText
{
get
{
return _contactText;
}
set
{
SetProperty(ref _contactText, value);
}
}
}
Page2ViewModel.cs
public class Page2ViewModel : BaseViewModel
{
public List<DataType> ListContacts
{
get;
set;
}
public List<Contacts> contacts { get; set; }
public Page2ViewModel()
{
ListContacts = FieldPicker.GetContacts().OrderBy(c => c.Value).ToList();
contacts = new List<Contacts>() {
new Contacts() {Key = 1, Contact_Name="Test_Name1",Contact_Address ="address1",Contact_eMail="testemail1@gmail.com",Contact_Mobile="Phone1:111111",ContactText="test1"},
new Contacts() {Key = 2, Contact_Name="Test_Name2",Contact_Address ="address2",Contact_eMail="testemail2@gmail.com",Contact_Mobile="Phone1:222222",ContactText="test2"},
new Contacts() {Key = 3, Contact_Name="Test_Name3",Contact_Address ="address3",Contact_eMail="testemail3@gmail.com",Contact_Mobile="Phone1:333333",ContactText="test3"},
};
}
private DataType _selectedContact;
public DataType SelectedContact
{
get
{
return _selectedContact;
}
set
{
SetProperty(ref _selectedContact, value);
refreshSelectedField(SelectedContact);
}
}
private void refreshSelectedField(DataType dataType) {
if (dataType.Contact_Type == (int)Type.Contact_Address) {
foreach (Contacts model in contacts)
{
model.ContactText = model.Contact_Address;
}
} else if(dataType.Contact_Type == (int)Type.Contact_eMail)
{
foreach (Contacts model in contacts)
{
model.ContactText = model.Contact_eMail;
}
}else if (dataType.Contact_Type == (int)Type.Contact_Mobile)
{
foreach (Contacts model in contacts)
{
model.ContactText = model.Contact_Mobile;
}
}
}
private string _contactText;
public string ContactText
{
get
{
return _contactText;
}
set
{
SetProperty(ref _contactText, value);
}
}
}
MainPage.xaml
<StackLayout>
<Label x:Name="LblDisplay" Text="Select a Field" FontSize="Medium" TextColor="Blue" />
<Picker Title="Select --" ItemsSource="{Binding ListContacts}" ItemDisplayBinding="{Binding Value}" SelectedItem="{Binding SelectedContact}" />
<ListView x:Name="ContactList" HasUnevenRows="True" IsVisible="True" ItemsSource="{Binding contacts}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" VerticalOptions="Center" >
<Label Text="{Binding Contact_Name}" TextColor="OrangeRed" />
<Label x:Name="LblField" Text="{Binding ContactText}" TextColor="OrangeRed" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.