Search Unity

Searchable ListView?

Discussion in 'UI Toolkit' started by Alex-Chouls, May 21, 2022.

  1. Alex-Chouls

    Alex-Chouls

    Joined:
    Mar 24, 2009
    Posts:
    2,657
    I have a bound list, where each item has an isVisible property updated by search and other factors.

    I added this to the bind method (ShowIf is an extension method that sets the display style):

    Code (CSharp):
    1.             ve.TrackPropertyValue(isVisibleProperty, prop =>
    2.             {
    3.                 ve.ShowIf(prop.boolValue);
    4.             });
    It works, but when I deselect the Inspector and reselect it, I get this error spam:

    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContextUpdater.Update () (at <fa4830dcb55b4d21a18ffd468621039e>:0)
    UnityEngine.UIElements.VisualTreeBindingsUpdater.UpdateBindings () (at <13f9cfbfcaea4d30a66e97eb510cfa62>:0)
    UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at <13f9cfbfcaea4d30a66e97eb510cfa62>:0)
    UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <13f9cfbfcaea4d30a66e97eb510cfa62>:0)
    UnityEngine.UIElements.Panel.UpdateBindings () (at <13f9cfbfcaea4d30a66e97eb510cfa62>:0)
    UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at <13f9cfbfcaea4d30a66e97eb510cfa62>:0)
    UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at <13f9cfbfcaea4d30a66e97eb510cfa62>:0)
    UnityEditor.RetainedMode.UpdateSchedulers () (at <fa4830dcb55b4d21a18ffd468621039e>:0)

    It seems like old bindings are not unbound and are still trying to run.

    I've tried adding these to Bind and Unbind ListView callbacks:
    ve.Unbind();
    ve.TrackPropertyValue(null);

    How do I clear all bindings on a ListView item?
    Is there a way to debug which binding is generating the error?

    Also:
    Are there any examples of a searchable bound ListView to look at?
    Hiding/showing items in the bind callback seems to confuse the ListView, sometimes resulting in duplicate or missing items. But maybe that's a side-effect of not properly un-binding?

    EDIT: It seems like hiding items in the bind callback confuses ListView. Setting height to zero works better, but feels a bit hacky. It would be nice if ListView had some kind of filter callback to select items to show.

    EDIT: So it seems setting item height to zero confuses the ListView in a different way, resulting in a content container size of the wrong height. I'm guessing its setting its size based on some layout that happens before the bind callback? Anyway, it's all feeling a little hacky. I'm guessing you're supposed to build a filtered list and use it as the item source, but then you have to re-implement all the binding for reordering etc. Searchable list views seem like a fundamental editor control, so it would be nice to have examples of how to build one properly.
     
    Last edited: May 21, 2022