Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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'.

Model Class
C#
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; }
}

XAML Page
XML
 <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>

Code Behind
C#
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;
    }
}

View Model Class
C#
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));
    }
}


What I have tried:

Tried to use picker, INotifyPropertyChanged
Posted
Updated 19-Nov-20 3:38am

1 solution

When you create your "item source collection", assign either the VALUE of "email" OR "address" to the "display field" (column), and just display what's in that column.

Easier than what you're trying to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900