ListBox Items
Last updated
Was this helpful?
Last updated
Was this helpful?
ListBoxes are one of the more complex controls in Gum Forms. This document discusses how to work with and customize ListBox items.
By default whenever an instance is added to the ListBox Items property, the ListBox creates a ListBoxItem internally to display the object. Notice that the following code adds integers to a ListBox's Items property which internally creates UI elements to represent the integers.
By default the ListBoxItem displays the ToString of whatever is added to the Items property. In the case of integers, the string representation of the integer is displayed. However, if we display an object, such as information about a weapon, the default display is not very useful.
For example, consider the following code:
In this case ToString is called on WeaponDefinition, but this doesn't provide useful information to the user:
We can change the displayed string by modifying the ToString method in WeaponDefinition.
By implementing a custom ToString method on the WeaponDefinition class we can customize how it is displayed in the ListBox. While this is handy, it does limit us because we may want to modify ToString for other purposes such as customizing information in the debugger.
We can customize the way WeaponDefinition is displayed without making any changes to WeaponDefinition itself. Instead we can create a new class that is responsible for converting a WeaponDefinition instance into a string to be displayed by each ListBoxItem.
To do this, we need to perform the following steps:
Create a new class that inherits from ListBoxItem. The purpose of this class is to convert our WeaponDefinition into a string.
Associate this class with our ListBox by assigning the ListBox's FrameworkElementTemplate property
The following code has been modified to create and use a WeaponDefinitionListBoxItem:
This code example creates a ListBoxItem named WeaponDefinitionListBoxItem. As the name suggests, it is specifically created to display WeaponDefinition instances. Of course, you could create a more generalized version of this class which might handle a variety of different types of items.
As mentioned above, the ComboBox control is similar to ListBox. For example, we can change the type from ListBox to ComboBox to get nearly identical behavior as shown in the following code block:
This code compiles and works mostly the way we want it, but not perfectly. Notice that WeaponDefinition's ToString is still called to display the main text on the combo box.
Fortunately we can create a class that is derived from ComboBox which overrides the UpdateToObject method just like we did earlier for ListBoxItem.
Now we can use this new ComboBox-deriving class:
Once items are added to a ListBox, the selection can be accessed through the SelectedObject property. For example, we could handle equipping the selected weapon using code similar to the following block:
Keep in mind that the ListBoxItem is responsible for converting what was added to the ListBox Items property into a string, but the Items and SelectedObject property are still referencing the WeaponDefinition instances.
This document shows how to work with ListBox and ComboBox Items. A future tutorial discusses how to further customize the appearance of ListBoxItems.
The next tutorial covers how to use various input hardware including the mouse, keyboard, and gamepad with Gum UI.