简体   繁体   English

Xamarin forms 从列表中设置选择器 SelectedItem

[英]Xamarin forms Set Picker SelectedItem From List

I am working with Xamarin.Forms and I am using Picker for a drop-down list.我正在使用Xamarin.Forms并且我正在使用Picker作为下拉列表。

I am trying to make a Picker display the book name that matches a string (for simplification purposes) from a ObservableCollection in a ViewModel .我正在尝试让 Picker 显示与ViewModel中的ObservableCollection中的字符串(为了简化目的)匹配的书名。

PageOne.xaml: PageOne.xaml:

<Picker x:Name="BookPicker" ItemDisplayBinding="{Binding Name}" ItemsSource="{Binding BookTypeList}"></Picker>

PageOne.cs: PageOne.cs:

public PageOne()
{
     InitializeComponent();
     this.BindingContext = new BookViewModel();

     string myBook = "c";

     for (int x = 0; x < 5; x++)
     {
          if (BookViewModel.BookTypeList[x].Name == myBook)
          {
               BookPicker.SelectedIndex = x;
          } 
     }
}

BookViewModel:书视图模型:

  public BookViewModel()
    {
        BookTypeList = new ObservableCollection<BookType>(){
            new BookType() { BookID = 0, Name = "a" },
            new BookType() { BookID = 1, Name = "b" },
            new BookType() { BookID = 2, Name = "c" },
            new BookType() { BookID = 3, Name = "d" },
            new BookType() { BookID = 4, Name = "e" },
        };
        bookType = BookTypeList[0];
    }

    public class BookType
    {
        public int BookID { get; set; }
        public string Name { get; set; }
    }

BookViewModel.BookTypeList results in this error: BookViewModel.BookTypeList导致此错误:

CS0120: An object reference is required for the nonstatic field, method, or property CS0120:非静态字段、方法或属性需要 object 引用

How can I iterate through the ObservableCollection in BookViewModel to find the ID that matches the string, so that I can set the Picker.SelectedIndex?如何遍历 BookViewModel 中的ObservableCollection以找到与字符串匹配的 ID,以便设置 Picker.SelectedIndex?

Thanks!谢谢!

You got that error because BookViewModel is not a static class, if you are looking to access BookTypeList you need to do that thru a BookViewModel instance您收到该错误是因为BookViewModel不是 static class,如果您要访问BookTypeList ,您需要通过BookViewModel实例进行操作

public PageOne()
{
     InitializeComponent();
     this.BindingContext = new BookViewModel();

     string myBook = "c";
     var vm = BindingContext as BookViewModel;
     for (int x = 0; x < 5; x++)
     {
          if (vm.BookTypeList[x].Name == myBook)
          {
               BookPicker.SelectedIndex = x;
          } 
     }
}

Also if you have a unique item in the ObservableCollection, you may replace your For loop with FindIndex :此外,如果您在 ObservableCollection 中有一个独特的项目,您可以用FindIndex替换您的 For 循环:

public PageOne()
{
     InitializeComponent();
     this.BindingContext = new BookViewModel();

     string myBook = "c";
     var vm = BindingContext as BookViewModel;
     BookPicker.SelectedIndex = vm.BookTypeList.FindIndex(x => x.Name.Equals(myBook));
}

Or SelectedItem instead of SelectedIndexSelectedItem而不是SelectedIndex

 BookPicker.SelectedItem = vm.BookTypeList.Find(x => x.Name.Equals(myBook));

If the item is not unique you may need to use Where clause/extension method from Linq如果项目不是唯一的,您可能需要使用Linq中的Where子句/扩展方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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