Xamarin表单SelectedItem在Picker中不可见

时间:2017-10-22 02:35:41

标签: xamarin xamarin.forms

我在将所选项目绑定到选择器方面遇到了一些困难。我已经通过各种答案,但没有一个适合我。这是我的代码

我的XAML

 `<Picker x:Name="mobListPicker" 
  Style="{StaticResource PickerOrangeStyle}" Title="Select Mob" 
                     
  HeightRequest="50" ItemsSource="{Binding ProductList, Mode=TwoWay}" 
  ItemDisplayBinding="{Binding ProductNo}" 
                  
  SelectedItem="{Binding SelectedProduct,Mode=TwoWay}">   
            
</Picker>`

所选项目的属性

    private Product _selectedProduct;
    public Product SelectedProduct
    {
        get { return _selectedProduct; }
        set { SetProperty(ref _selectedProduct, value); }
    }

如果有人可以帮我识别错误,那就太棒了。

1 个答案:

答案 0 :(得分:0)

我制作了一个关于使用Picker和MVVM绑定的简单演示项目。作为商品来源,我使用ObservableCollection类型的Person

当用户从Picker中选择一个项目时,下面标签的值/内容会发生变化。请看一下我认为这个项目是您解决问题所需要的。

您没有向我们提供您的整个代码,因此很难看到您可能的错误。也许这对你有帮助。

演示:

enter image description here

代码段:

我的Person类看起来像这样:

public class Person {

    public int PersonId { get; set; }

    public string FName { get; set; }
    public string LName { get; set; }

    public string FullName { get { return $"{FName} {LName}"; } }
    public string Biography { get; set; }

}

我的ViewModel:

public class AllPersonsViewModel : INotifyPropertyChanged{

    public ObservableCollection<Person> AllPersons { get; set; }

    private Person selectedPerson;
    public Person SelectedPerson {
        get { return selectedPerson; }
        set { selectedPerson = value;
            OnPropertyChanged();
        }
    }

    public AllPersonsViewModel() {

        AllPersons = new ObservableCollection<Person>() {

            new Person() {
                Biography = "Lorem ipsum person 1",
                FName = "Person",
                LName = "One",
                PersonId = 1,
            },

            new Person() {
                Biography = "Lorem ipsum person 2",
                FName = "Person",
                LName = "Two",
                PersonId = 2,
            },

             new Person() {
                Biography = "Lorem ipsum person 3",
                FName = "Person",
                LName = "Three",
                PersonId = 3,
            },

        };


    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

我有一个视图将此视图模型用作BindingContext

...我的页面中带有Picker控件的XAML在这里:

<?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="XF.Picker.Demo.Views.PersonsPage">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">

            <Picker Title="All person"
                    ItemDisplayBinding="{Binding FullName}"
                    ItemsSource="{Binding AllPersons}"
                    SelectedItem="{Binding SelectedPerson}"/>

            <!-- Margin just to see picker dropdown and labels at the same time-->
            <Label Text="{Binding SelectedPerson.FullName}" Margin="0,80,0,0"/>
            <Label Text="{Binding SelectedPerson.Biography}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我在github上发布了这个代码项目你可以找到它here