繁体   English   中英

xamarin forms 选择器值的正确绑定方法

[英]Correct way of binding xamarin forms picker value

我的问题是我有 2 个选择器控件。 这些选取器控件中的 1 个绑定到列表 a,这些选取器控件之一绑定到列表 b。 这两个控件都显示了我需要它们的数据,并且正确的显示绑定工作正常。

我的问题是当我绑定 selectedItem 属性时,它适用于列表 a,但不适用于列表 b。 我已经检查过该代码实际上是彼此相似的副本。

我一直在使用同步融合 Combobox 但切换到选择器,因为我认为这里有问题但没有。 无论发生什么,都完全取决于我在做什么。

使用场景是我从我的 API 中提取付款类型列表,并基于此填充选择器。 这行得通。

我的主视图的数据源包含一个 ID。 当我修改记录时,我运行一个名为 update 的方法来查找 selectedItem。 我对这种方法不满意,并且有兴趣看看其他人使用什么。

update 方法获取选择器的数据源并找到我期望的选定项。 这也可以正常工作,但不绑定。

 [Serializable]
    public class PaymentInformation :BaseModel 
    {

        public int? ID { get; set; }

        public DateTime? StartDate { get; set; }

        public DateTime? EndDate { get; set; }

        public int? PaymentTypeId { get; set; }

        public string PaymentTo { get; set; }

        public string Location { get; set; }

        public string Notes { get; set; }

        public PersonInformation PersonBudget { get; set; }

        public decimal AmountPaid { get; set; }

        public decimal AmountReceived { get; set; }

        public double TotalHours { get; set; }

        public void Update(ObservableCollection<ResourceInformation> resources , ObservableCollection<PaymentTypeInformation> paymentTypes)
        {
           if(PaymentTypeId != null) this.PaymentTypeInformation1 = paymentTypes?.FirstOrDefault((paymentType) => paymentType.ID == PaymentTypeId.Value);
            this.Resource = resources?.FirstOrDefault((resource) => resource.ResourceId == PersonBudget?.ID);
        }

        private PaymentTypeInformation _paymentTypeInformation;

        private PaymentTypeInformation PaymentTypeInformation1 { get { return _paymentTypeInformation; } set { _paymentTypeInformation = value; OnPropertyChanged(nameof(PaymentTypeInformation1)); } }

        private ResourceInformation _resource;

        public ResourceInformation Resource { get { return _resource; } set { _resource = value; OnPropertyChanged(nameof(Resource)); } }

    }

底层 xaml 是:

 <Label Grid.Row="8" Grid.Column="0" Text="Payment Type:" />
            <Picker BackgroundColor="White" Grid.Row="8" Grid.Column="1" ItemsSource="{Binding PaymentTypesDataSource}"  ItemDisplayBinding="{Binding Path=DisplayText}" IsEnabled="{Binding IsProcessing, Converter={StaticResource reverseBoolConvertor}}" SelectedItem="{Binding DataSource.PaymentTypeInformation1, Mode=TwoWay}" />

预期的结果是下拉菜单使用它没有初始化的 selectedItem 进行初始化(在一个使用场景中 - 另一个工作正常)。

只见树木不见森林。

private PaymentTypeInformation PaymentTypeInformation1
{ 
    get
    {
        return _paymentTypeInformation;
    }
    set
    { 
        _paymentTypeInformation = value;
        OnPropertyChanged(nameof(PaymentTypeInformation1));
    } 
}

无法绑定到私有属性 - 更改为公共并立即工作。 坚持了一天,就像相信的那样疯狂。

暂无
暂无

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

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