【问题标题】:Xamarin Forms SelectedItem not visible in PickerXamarin Forms SelectedItem 在选取器中不可见
【发布时间】:2018-04-02 20:34:10
【问题描述】:

我在将所选项目绑定到选择器时遇到了一些困难。我经历了各种答案,但没有一个对我有用。这是我的代码

我的 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); }
    }

如果有人能帮我找出错误,那就太棒了。

【问题讨论】:

  • 你也应该发布你的模型和视图模型
  • 你能删除 ItemsSource 中的 Mode 和 Update Mode=SelectedItem 中的默认

标签: xamarin xamarin.forms


【解决方案1】:

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

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

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

演示:

代码sn-ps:

我的 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; }

}

我的视图模型:

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

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 2022-01-07
    • 2020-11-21
    • 1970-01-01
    • 2018-01-03
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多