change PopupField choices programmatically

Hi there,

Choices field of PopupField is not exposed which means i cant set it AFTER it is created.
So please provide how can i solve this?

One work around is to delete and then recreate but then how do i delete an already created popup field?

Thanks

Deleting an element should be as simple as removing it from the hierarchy using el.RemoveFromHierarchy() or parent.Remove(child);

1 Like

Thanks,
I really hope they provide to change choices after popup is created as deleting creating is a bad workaround.

There’s good news there! Creating your own popupfield is quite trivial and is likely a good starter project if you’re just getting your feet wet with uielements.

    public class BrowserPopupField<T> : BaseField<T>, IBrowserPopupField {
public BrowserPopupField(string label) : base(label, new VisualElement() { name = "sr-visual-element" }) {
            PopupWindowText = string.Format("Select {0}...", label);
            PopupWindowSize = new Vector2(300, 400);
            this.AddClasses("unity-base-popup-field", "unity-popup-field");

            visualElement = this.Q("sr-visual-element");
            visualElement.AddClasses("unity-base-field__input", "unity-base-popup-field__input", "unity-popup-field__input");

            popupLabel = new Label();
            visualElement.AddClasses("unity-text-element", "unity-base-popupfield-text");
            popupLabel.style.flexGrow = 1;
            popupLabel.pickingMode = PickingMode.Position;
            popupLabel.text = EmptyLabelText;

            popupLabel.RegisterCallback<MouseDownEvent>(e => {
                if (e.button == 0 && e.clickCount == 1) {
                    e.StopPropagation();
                }
            });

            popupLabel.RegisterCallback<ClickEvent>(e => {
                if (e.button == 0) {
                    e.StopPropagation();
                    OpenPopupContent();

                }
            });
            Add(popupLabel);

            visualElement.Add(popupLabel);

            var dropdown = new VisualElement();
            dropdown.AddClasses("unity-base-popup-field__arrow");
            visualElement.Add(dropdown);
        }

Here’s the constructor from mine - will help you get the visual styles down as well as the click events. From there you just need to display a popup or context menu.