Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Data Binding to Picker

HOAKSHOAKS Member ✭✭

I am trying to make a drop-down list in Xamarin.Forms that allows a user to select a currency symbol of their choosing. I would like the first element of CurrencyList to be the default selected symbol.

I am having difficulty trying to implement this. I have the following error with this line of code in CurrencyViewModel.cs: base.RaisePropertyChanged(nameof(CurrencyList));

Error: CS0117: 'object' does not contain a definition for 'RaisePropertyChanged'.

Page1.xaml:

<Label Grid.Row="2" Grid.Column="0" Text="Currency Symbol" Margin="5,0,0,0" Style="{StaticResource baseLabelBold}" />
<Picker Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="3" ItemDisplayBinding="{Binding CurrencySymbol}" x:Name="CurrencyPicker" ItemsSource=" {Binding CurrencyList}" SelectedItem="{Binding SelectedCurrency,Mode=TwoWay}"></Picker>

Currency.cs:

namespace AppProject.Models
{
    public class Currency
    {
        public int CurrencyID { get; set; }
        public string CurrencySymbol { get; set; }
    }
}

CurrencyViewModel.cs:

using AppProject.Models;
using Prism.Navigation;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;

namespace AppProject.ViewModels
{
    public class CurrencyViewModel : INotifyPropertyChanged
    {

        private ObservableCollection<Currency> _currencylist;
        public ObservableCollection<Currency> CurrencyList
        {
            get { return _currencylist; }
            set
            {
                _currencylist = value;
                base.RaisePropertyChanged(nameof(CurrencyList));
            }
        }


        private CurrencyList _SelectedCurrency;

        public event PropertyChangedEventHandler PropertyChanged;

        public CurrencyList SelectedCurrency
        {
            get { return _SelectedCurrency; }
            set
            {
                _SelectedCurrency = value;

                base.RaisePropertyChanged(nameof(SelectedCurrency));
            }
        }

        protected async Task CurrencyList_Completed()
        {
            _currencylist = new ObservableCollection<CurrencyList>
            {
                new CurrencyList() { CurrencyID = 1, CurrencySymbol = "£" },
                new CurrencyList() { CurrencyID = 2, CurrencySymbol = "$" },
                new CurrencyList() { CurrencyID = 3, CurrencySymbol = "$" }
            };
            CurrencyList = _currencylist.ToObservableCollection();
            SelectedCurrency = CurrencyList.Where(x => x.CurrencyID == 1).FirstOrDefault();

        }

        protected override async Task Initialize(NavigationParameters parameters)
        {
            if (parameters.GetNavigationMode() == NavigationMode.New)
            {

                CurrencyList_Completed();
            }

        }
    }
}

I am struggling to implement the ability to have the list of currency symbols be bound to the Picker and the first symbol "£" be the default element that is selected on loading the page.

How can I fix this? Help would be greatly appreciated.

Sign In or Register to comment.