Quote:I'm trying to use a single Label to display one of the two data fields alternately in Xamarin Forms. Only Label 1 Displaying the binding field (Contact_Name), while second Label which I am trying to use a variable "DisplayField" is not displaying either 'Contact_Address' or 'Contact_eMail'.
public class Contacts { [PrimaryKey][AutoIncrement] public int Contact_ID { get; set; } public string Contact_Name { get; set; } public string Contact_Address { get; set; } public string Contact_eMail { get; set; } }
<StackLayout> <Button Text="Display Address" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Fill" Clicked="Display_Address" /> <Button Text="Display Email" FontSize="Large" HorizontalOptions="Center" VerticalOptions="Fill" Clicked="Display_eMail" /> <Entry HorizontalOptions="FillAndExpand" Text="{Binding DisplayField}" /> <ListView x:Name="listView" HasUnevenRows="True" > <ListView.ItemTemplate> <DataTemplate> <ViewCell > <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand" > <Frame > <StackLayout Orientation="Vertical" VerticalOptions="Center"> <Label Text="{Binding Contact_Name}" FontSize="Medium" LineBreakMode="WordWrap" /> <Label Text="{Binding DisplayField}" LineBreakMode="WordWrap" /> </StackLayout> </Frame> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>
public partial class FieldSwap : ContentPage { readonly FieldViewModel _fieldViewModel; readonly SQLiteAsyncConnection _connection = DependencyService.Get<ISQLite>().GetConnection(); public ObservableCollection<Contacts> CList { get; set; } public static string DisplayField { get; private set; } public static int caseSwitch { get; private set; } public FieldSwap() { InitializeComponent(); _fieldViewModel = new FieldViewModel(); _fieldViewModel.Field = "Contact_Address"; this.BindingContext = _fieldViewModel; } public static void SelectField() { switch (caseSwitch) { case 1: DisplayField = "Contact_Address"; break; case 2: DisplayField = "Contact_eMail"; break; default: DisplayField = ("Contact_Address"); break; } } private void Display_Address(object sender, EventArgs e) { caseSwitch = 1; SelectField(); ReadData(); } private void Display_eMail(object sender, EventArgs e) { caseSwitch = 2; SelectField(); ReadData(); } public void ReadData() { var list = _connection.Table<Contacts>().ToListAsync().Result; CList = new ObservableCollection<Contacts>(list); listView.ItemsSource = CList; } }
public class FieldViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; String _displayField; public string DisplayField { set { if (!value.Equals(_displayField, StringComparison.Ordinal)) { _displayField = value; OnPropertyChanged("DisplayField"); } } get { return _displayField; } } void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)