Dropdown will not change selected item text after changing options list at runtime
I have a Dropdown UI control. If I clear the options list and repopulate it, the text of the selected item is not changed.
It doesn't matter where or what the user clicks on, the only way to force the text to change is to alter the value member to a different value, and then put it back. This occurs when there is only one item in the new list, and when there is more than one. I have tried explicitly invoking OnValueChanged() but this still has no effect.
How do I force the Dropdown to reset/repaint/remove whatever caching its got after updating the options list?
I am resetting the options list like so:
m_branchesDropdown.options.Clear();
foreach (var branch in m_branches)
{
m_branchesDropdown.options.Add(new Dropdown.OptionData() { text = branch.name });
}
m_branchesDropdown.RefreshShownValue ();
worked for me with little extra work to add the value to a string variable, I used string string optionselected = m_branchesDropdown.captionText.text
Answer by brandonhotdo · Nov 16, 2015 at 08:52 PM
I had this problem too but this seemed to fix it:
int TempInt = m_branchesDropdown.value;
m_branchesDropdown.value = m_branchesDropdown.value+1;
m_branchesDropdown.value = TempInt;
Just put that after the foreach loop.
This is the solution I used. I was concerned that Unity required an iteration between setting it to another value then setting it back, but it doesn't so this work around is very unobtrusive!
Sadly this workaround doesn't work if you only have one value in the new options list.
For me that's not the perfect solution because changing .value triggers the OnValueChanged event. I used "SetValueWithoutNotify" instead, and it worked.
Answer by viju · Feb 02, 2016 at 01:24 PM
m_branchesDropdown.RefreshShownValue ();
worked for me with little extra work to add the value to a string variable, I used string string optionselected = m_branchesDropdown.captionText.text
Answer by elliselkins · Sep 07, 2016 at 09:57 PM
There is a function called RefreshShownValue() on the dropdown that should be called.
THAN$$anonymous$$ YOUUUUUUUUUUUUUUU!!!! I had the dropdown filled by script and the default selected item was not triggering any event, adding so it did. coool.
Answer by flipwilson · Jul 12, 2016 at 05:34 PM
ddProperty.value = 0;
ddProperty.Select();
ddProperty.RefreshShownValue();
Answer by neural · Nov 22, 2015 at 04:55 PM
Ok just found the solution to this:
m_branchesDropdown.transform.FindChild("Put_Your_Caption_Text_Here").GetComponent<Text>().text = m_branchesDropdown.options[m_branchesDropdown.value].text;