how to stop TextField RegisterValueChangedCallback firing when setting the label of the TextField?

Hi, I am having this weird problems with callbacks.

I am writing some code with UIElements where I am in need of doing things to text when the user changes the value of a TextField - however, it seems the callback is also firing when the label of the textField changes. For my particular case, this is a bit cumbersome, because it breaks things for me. Is there a way to only get the callback when the value changes (not the label), or any way we can discern in the callback, who it is destined for?

_factFilterField.label = "FactFilter";
_factFilterField.RegisterCallback<ChangeEvent<string> >(evt =>
                {
                    Debug.Log($"FactFilter We are called with {evt.newValue}");
                    UpdateListView();
                });
_factFilterField.label = "FactFilter 2";

This code actually prints both FactFilter and FactFilter 2 , which is weird, but I guess can come from not Unregistering the callback. This is though besides the issue I am having - which is, a change in the label text of a textField should not cause a ChangeEvent where it is not possible to know if the label or value was changed.

RegisterValueChangedCallback has the exact same behaviour.

_factFilterField.RegisterValueChangedCallback(evt =>
                {
                   
                    Debug.Log($"FactFilter We are called with {evt.newValue}");
                    UpdateListView();
                });

This is on
2022.2.1f1.112.6257
Revision: 2022.2/release 4fead5835099

I tried looking at the documentation to see what is expected behaviour, but I did not find anything that spelled it out for me. I hope someone on here can help me.

I can think of two ways of doing this. One is to check the target property of the ChangeEvent and ignore it when it’s a Label. The other is to get the labelElement from your TextField, cast it to INotifyValueChanged, and change it’s value with SetValueWithoutNotify; that will change the field’s label without emitting a ChangeEvent.

1 Like

Thank you, casting labelElement to INotifyValueChanged and calling SetValueWithoutNotify , worked. the target/currentTarget looked like it was only TextField in the callback, but anyhow, I got what I needed, big thanks!

Sorry to necro this thread, but I just had an issue with this Event being triggered when changing the Label by my localization code.

Is this really the expected behavior of RegisterValueChangedCallback? (like, it says VALUE in the name of the event)

Same here. Unity 2021.3.01f casting labelElement to INotifyValueChanged helps with text input value, but gives me empty label.