Search code examples
xamarinxamarin.iosxamarin.androidxamarin.formsxamarin-studio

Using Messaging center xamarin forms PCL to set current objects value


I have a scenario where i create Entry Controls programmatically.

     foreach (var control in FormInfo.FormElementsInfo)
        {

       case "textbox":
                      //Some code
                      break;


       case "dropdown":

       Entry objDropdown = new Entry();
       objDropdown.HeightRequest = 40;
       objDropdown.StyleId = Convert.ToString(control.ElementId);
       objDropdown.SetBinding(Entry.TextProperty, "ElementValue",BindingMode.TwoWay);
       objDropdown.BindingContext = control;
       layout.Children.Add(objDropdown);

       MessagingCenter.Subscribe<Picklists, string>(objDropdown, "PicklistSelected", (sender, arg) =>
        {
           objDropdown.Text = arg; 
    // I tried this too as this is two way binding. It didn't show the value. 

          //control.ElementValue = arg;



        } );
       break;
     }

If i click on any entry it will open me a list view. Once i select the option in the list view it will populate that data in the Entry. But this should show the selected value only in the current entry but it is changing the value in all the entry's.

How to avoid this situation. I want the selected value to be populated only in the current entry.

Any suggestion would be appreciated. Thank you.

=== More clear question=====

If we create n number of Entry controls programmatically with 2 way binding . Is it possible to change the single entry value on selecting something in other page? If yes how to achieve this?


Solution

  • First of all Thank you @Joshua Poling for taking time to help me.

    I think MessagingCenter is not suitable for this approach.

    I am assigning a unique styleId to each element that i create.That basically stores the position in the stack layout.

    I have written a delegate which returns the selected value and also the position of the element. As the element is always an Entry that fires this event. I used the below code to achieve this.

    Entry myentry = (Xamarin.Forms.Entry)layout.Children[src.ElementId];