简体   繁体   中英

Xamarin.Forms, picker, issue with displaying SelectedItem

I have Xamarin.Forms application, have two pickers on page. Then I have view model for this page:

public class ExpenseEntriesViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ExpenseEntriesViewModel()
    {
        // getting data from server here

        if (ExpenseEntriesDateRangeInfo.AvailableYears.Count > 0)
        {
            // AvailableYears is List<int>
            SelectedYear = ExpenseEntriesDateRangeInfo.AvailableYears.Max();
        }

        SelectedMonth =
            // AvailableMonths is List<MonthValue>
            ExpenseEntriesDateRangeInfo.AvailableMonths.FirstOrDefault(i => i.MonthNumber == DateTime.Today.Month);
    }

    public DateRangeInfo ExpenseEntriesDateRangeInfo { get; set; }

    private int _selectedYear;
    public int SelectedYear
    {
        get => _selectedYear;
        set
        {
            _selectedYear = value;
            OnPropertyChanged();
        }
    }

    private MonthValue _selectedMonth;
    public MonthValue SelectedMonth
    {
        get => _selectedMonth;
        set
        {
            _selectedMonth = value;
            OnPropertyChanged(nameof(SelectedMonth));
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Here is the MonthValue model:

public class MonthValue
{
    public int MonthNumber { get; set; }
    public string MonthName { get; set; }
}

Models and data are correct, I went through code with debugger and checked. XAML markup of pickers:

<Picker Title="Month"
                        HorizontalOptions="FillAndExpand"
                        ItemsSource="{Binding ExpenseEntriesDateRangeInfo.AvailableMonths}"
                        ItemDisplayBinding="{Binding MonthName}"
                        SelectedItem="{Binding SelectedMonth, Mode=TwoWay}"/>
<Picker Title="Year"
                        HorizontalOptions="FillAndExpand"
                        ItemsSource="{Binding ExpenseEntriesDateRangeInfo.AvailableYears}"
                        SelectedItem="{Binding SelectedYear, Mode=TwoWay}"/>

Year picker binds to list of int, works perfect. Month picker works with MonthValue array and do not displaying MonthName property as SelectedItem name, this is the issue. Is there any solution or thoughts? Pretty simple case as for me.

UPDATE. Added project with issue demo here

It tested on real android device (Android 6.0, API 23)

Binding in Xamarin.Forms works not so obvious and require some boilerplate code.

I add AvailableMonths and AvailableYears properties directly to ExpenseEntriesViewModel and typed as ObservableCollection and ObservableCollection accordingly and it works now as expected.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM