Combo Box Reload

321
1
02-09-2018 09:36 AM
Highlighted
by
Occasional Contributor II

I am using an ESRI Combo Box in my toolbar.  This Combo Box is loaded with the IDs of whatever polygons are in my map.  When a user switches to another location, with a whole new set of polygons, I want my Combo Box to update itself.  The problem is that I can't seem to figure out how to run my Update/Populate logic from outside of the combobox code.  I need to be able to tell the combo box from other functions to revalidate or reinitialize itself?  I think I need to make some static reference to this function, but can't seem to be able to accomplish this?  The below code works initially, but when the user switches, i need to be able to call updateCombo from anywhere, or just tell the combo to reload.

        public GoToCurrent()
        {
            UpdateCombo();
        }      

        public async void UpdateCombo()
        {
            try
            {
                Enabled = false;

                Clear();
                SortedSet<string> FieldIdList = await FeatureServiceManagement.getFeatureLayerUniqueList("Current Field", "", "CurrentFieldId");

                if (!FieldIdList.IsNullOrEmpty())
                {
                    foreach (string fieldID in FieldIdList)
                    {
                        Add(new ComboBoxItem(fieldID));
                    }
                }

                Enabled = true;
            }
            catch (Exception e)
            {
                logError("UpdateCombo", e);
            }
        }
Reply
0 Kudos
1 Reply
Highlighted
Regular Contributor

Hi,

You can make property in your module class to set combo box. On startup you need from combo box class to set that property to combo box itself. When you need to update combo box content you will find your module and read combobox property and call "UpdateCombo" method.

    public class MyModule : Module
    {
        private static MyModule _this = null;
        private MyComboBox _combo = null;
       
        internal MyModule()
        {
            _this = this;
        }

        /// <summary>
        /// Retrieve the singleton instance to this module here
        /// </summary>
        public static MyModule Current
        {
            get
            {
                return _this ?? (_this = (MyModule)FrameworkApplication.FindModule("My_Module"));
            }
        }

        public MyComboBox PolygonCombo
        {
            get
            {
                return _combo;
            }
            set
            {
                _combo = value;
                _combo.UpdateCombo();
            }
        }
    }

public class MyComboBox : ComboBox

{

 

   /// <summary>

   /// Constructor to initialize the ComboBox

   /// </summary>

   public MyComboBox()

   {

      MyModule modOwner = MyModule.Current;

      if (modOwner == null) return;

 

      modOwner.PolygonCombo = this;

   }

}

Another way is to use some kind of messaging. I use that approach in my applications:

https://stackoverflow.com/questions/23798425/wpf-mvvm-communication-between-view-model