xamarinxamarin.formsmvvmviewmodelfreshmvvm

Xamarin: Bind picker selected item with FreshMvvM ViewModel


I'm creating a form where a product status needs to be selected with a dropdown menu. I've created a picker for this. The data is loaded from a list in my ViewModel, but it doesn't get sent back. I've tried the same using entry fields and that works fine. I just have no idea how to link the picker with the view model.

Here's my code.

Xaml

    </Grid>
                    <Label Text="Current status" Style="{StaticResource MainLabel}"/>
                    <Label Style="{StaticResource MainLabel}" Text="{Binding ProductionStatus, Mode=TwoWay}"/>
                    <!-- gets send when saved-->
                    <Entry Style="{StaticResource MainEntry}" Text="{Binding ProductionStatus, Mode=TwoWay}" Keyboard="Text" /> 
                    <Label Text="Remark" Style="{StaticResource MainLabel} "/>
                    <!-- gets send when saved-->
                    <Entry Text="{Binding Remark}" Keyboard="Text" Style="{StaticResource MainEntry}"/>
                    <!-- Does not bind with anything.-->
                    <Picker x:Name="statusPicker" ItemsSource="{Binding ProductionOrderStatusName}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding ProductionStatusName}"/>
                   
    
                    <Button Style="{StaticResource PrimaryButton}" Text="Save"  Command="{Binding SaveCommand}"></Button>

Code-behind ViewModel

 public ICommand SaveCommand
        {
            get
            {
                return new Command(ExecuteSaveCommand);
            }
        }
        
        private async void ExecuteSaveCommand()
        {
            int statusId = FindProductionOrderStatusId(ProductionStatus); //the production status should be the result of the selected value in the picker
            Guid id = _CurrentProductionOrder.Id;
            string remark = Remark; // value from the remark entery in the xaml
          
            await __productionOrderService.UpdateAsync(id, remark,statusId);
        }

Properties

 
        public ObservableCollection<ProductionOrderStatus> productionOrderStatusName;
        public ObservableCollection<ProductionOrderStatus> ProductionOrderStatusName
        {
            get { return productionOrderStatusName; }
            set
            {
                productionOrderStatusName = value;


            }
        }
        public int amount;
        public int Amount
        {
            get { return amount; }
            set
            {
                amount = value;

            }
        }

        public DateTime finishDate;
        public DateTime FinishDate
        {
            get { return finishDate; }
            set
            {
                finishDate = value;
               

            }
        }
        public int ordernumber;
        public int OrderNumber
        {
            get { return ordernumber; }
            set
            {
                ordernumber = value;
             

            }
        }

        public string remark;
        public string Remark
        {
            get { return remark; }
            set
            {
                remark = value;
              

            }
        }

        public string productionStatus;
        public string ProductionStatus
        {
            get
            {
                return productionStatus;
            }
            set
            {
                productionStatus = value;
            

            }
        }

        private string materialNumber;

        public string MaterialNumber
        {
            get { return materialNumber; }
            set
            {
                materialNumber = value;
               

            }
        }


        private string materialDescription;

        public string MaterialDescription
        {
            get { return materialDescription; }
            set
            {
                materialDescription = value;
               

            }
        }

Code behind loading my data

 public OrderViewModel()
        {
            _productionOrderStepService = new MockingProductionOrderStepService();
            _materialService = new MockingMaterialService();
            __productionOrderService = new MockingProductionOrderService();
            __productionOrderStatusService = new MockingProductionOrderStatusService();
            _seederService = new Seeder(__productionOrderService, _productionOrderStepService, __productionOrderStatusService, _materialService);
            _seederService.EnsureSeeded();
           

        }

       

        public override void Init(object initData)
        {
            _CurrentProductionOrder = initData as ProductionOrder;
            LoadItemState();
            base.Init(initData);
        }

        private void LoadItemState()
        {
            ObservableCollection<ProductionOrderStatus> ProductionStatusName = new ObservableCollection<ProductionOrderStatus>(__productionOrderStatusService.GetListAllAsync().Result);
            this.ProductionOrderStatusName = ProductionStatusName;
            this.materialDescription = FindMaterialDescription(_CurrentProductionOrder.MaterialId);
            this.materialNumber = FindMaterialNumber(_CurrentProductionOrder.MaterialId);
            this.productionStatus = FindProductionOrderStatus(_CurrentProductionOrder.StatusId);
            this.remark = _CurrentProductionOrder.Remark;
            this.amount=_CurrentProductionOrder.Amount;
            this.finishDate = _CurrentProductionOrder.FinishDate;
            this.ordernumber = _CurrentProductionOrder.OrderNumber;

        }

Thx for the help!


Solution

  • you are making this more complicated than it needs to be

    <Picker x:Name="statusPicker" 
    
     // this is the List of items X to display
     ItemsSource="{Binding ProductionOrderStatusName}" 
     // this tells the picker which property of X to display to the user
     ItemDisplayBinding="{Binding Name}" 
     // this is the specific X the user has selected
     SelectedItem="{Binding SelectedStatus}" />
    

    then in your VM

     public ObservableCollection<ProductionOrderStatus> ProductionOrderStatusName { get; set; }
    
     public ProductionOrderStatus SelectedStatus { get; set; }