Picker bind source to list and select values from another object

Johny Unix 1 Reputation point
2020-12-18T11:56:59.407+00:00

I want to bind my Picker to clients list AllClients, nevertheless i want value selected there to be refelecting other object: selectedOrder which contains diffrent id and value than AllClients lists properties. Let me show below the code to clarify things.

This is ViewModel related to my page:

public class OrdersViewModel : BaseViewModel, IOrderViewModel
{
        public ObservableCollection<OrderReadModel> Orders { get; set; }
        public ObservableCollection<IdValueReadModel> AllClients { get; set; }
        private OrderReadModel _selectedOrder;

        public OrderReadModel SelectedOrder
        {
            get => _selectedOrder;
            set => SetValue(ref _selectedOrder, value);
        }

        public OrdersViewModel()
        {
           var clients = await _webApiService.GetBootleClientsIdValue(new CancellationToken());
            var listkl = (List<IdValueReadModel>)clients ;

            if (listkl != null)
                foreach (var item in listkl)
                    AllClients.Add(item);
        }
}

My _selectedOrder (OrderReadModel) contains ClientId and ClientName which will be equivalent to one of the values from AllClients (Id, Value).

Therefore on on my page i want to fill picker with my clients using AllClients and select current selection to the selection from _selectedOrder. So somehow it has to know that ClientId and ClientName from _selectedOrder is the same as Id and Value on AllClients. How can i do that? I tried as follows so far but doesn't work.

<Picker Title="My client" ItemsSource="{Binding AllClients}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}" ItemDisplayBinding="{Binding SelectedOrder.ClientName}" SelectedIndex="{Binding SelectedOrder.ClientId}"/>

Hope you got my point.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,315 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2020-12-18T13:07:41.477+00:00

    SelectedIndex="{Binding SelectedOrder.ClientId}" it's wrong, remove it


  2. JarvanZhang 23,951 Reputation points
    2020-12-21T07:15:18.3+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Picker bind source to list and select values from another object

    The ItemDisplayBinding is the property that will be displayed for each object in the list of items. The item is the 'OrderReadModel' class in your case, set data binding with the property directly.

    Check the code:

       <Picker Title="My client"   
               ItemsSource="{Binding AllClients}"   
               SelectedItem="{Binding SelectedOrder, Mode=TwoWay}"   
               ItemDisplayBinding="{Binding ClientName}"/>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments