Xamarin跨平台选择器未正确绑定

时间:2019-02-17 00:26:10

标签: c# xaml xamarin

我是Xamarin跨平台应用程序的新手

我试图将Picker ItemSource绑定到List并显示一个属性,但没有成功!我的参考文献是from here

请告诉我我的错误在哪里,请查看我的View或Xmal(或可能两者)

该列表是定义为的StdGrades列表

namespace FitRestults_Dev1
{
    class StdGrade 
    {
        public string Gradelbl
        { get; set; }
        public string Grade
        { get; set; }

        public static List<StdGrade> Grades()
        {
            List<StdGrade> GradesList = new List<StdGrade>(){
                new StdGrade(){ Gradelbl="10th Gup (White belt)", Grade="G10"},
                new StdGrade(){ Gradelbl="9th Gup (Organge belt)", Grade="G9"},
                new StdGrade(){ Gradelbl="8th Gup (Organge belt 1 tag)", Grade="G8"},
               ... };

                return GradesList;

        }  

            public List<StdGrade> GradesList => Grades();


            public static string GetGrade(string Input)
        {
            List<StdGrade> GradesList = Grades();
            var result = (from r in GradesList where r.Gradelbl == Input select r).First();

            return result.Grade;


        }

    }

对于“内容”页面,我将简单视图定义为

    namespace FitRestults_Dev1
{
    class AddStudentView 
    {
        List<StdGrade> _GradeList;

            public List<StdGrade> GradeList

        { get => _GradeList;
            set
            {
                _GradeList = StdGrade.Grades();

            }
        }
    }
}

我的内容页面xmal是

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FitRestults_Dev1.AddStudent"
             xmlns:src="clr-namespace:FitRestults_Dev1"
             >
     <ContentPage.BindingContext>
            <src:AddStudentView/>
        </ContentPage.BindingContext>
    <ContentPage.Content>

        <StackLayout Padding="10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Grid>
                …
                <Picker x:Name="GradePicker" Title="Select a Grade" Grid.Row="2" Grid.Column="1" MinimumWidthRequest="100" FontSize="12"
                     ItemsSource="GradeList" SelectedIndex="0" ItemDisplayBinding="{Binding Gradelbl}">
                </Picker>
            </StackLayout>
    </ContentPage.Content>
</ContentPage>

1 个答案:

答案 0 :(得分:2)

您提供的itemSource应该是错误的绑定方式

  ItemsSource={Binding GradeList}

还要停止使用Generic.List进行绑定,带有Xamarin Forms的MVVM应该具有ObservableCollections,因为它继承自INotifyPropertyChangedINotifyCollectionChanged