簡體   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