Handling Property Changes in a Spark Custom ItemRenderer
I recently got asked about how I store and retrieve data within a Spark DataGroup using a custom ItemRenderer, with a particular focus on how to monitor property changes. There are a few ways to achieve this, particularly when it comes to storing and retrieving data, here is one simple example using a CheckBox within a custom ItemRenderer and observing the total selected CheckBoxes.
Here I will detail the main snippets of functionality, download the actual source (links at bottom of the post) to get the full picture.
I tend to programme more in ActionScript as opposed to mxml, this is just my personal preference, however, lets do things a bit different and create a ValueObject using mxml and bind the selectedItems property to the selectedItems getter within custom List:
1 2 3 4 5 6 7 8 9 10 11
In a custom List create a Bindable getter that the VO selectedItems property (see above) is bound to:
1 2 3 4 5
In the List create a handler for the dataChange Event:
1 2 3 4
Still within the List add a method to loop through the data provider to retrieve all the currently selected items. This will change the selectedItems total count and update bindings by dispatching a propertyChange Event:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Within the custom ItemRenderer a data Object is going to be assigned a custom VO named Item with the following properties:
In the ItemRenderer assign a handler to the CheckBox change Event and update the data’s itemSelected property. If the itemSelected property has changed then dispatch a dataChange Event, this will ensure all the Bindings are kept in sync thus updating our custom VO in the main class.
1 2 3 4 5 6
This is a simple example as I stated above and there are many ways to achieve the final goal.
View the example [wpfilebase tag=file id=1]