Product Version | 2018.3 911 |
Product | RadTreeView for WinForms |
ToggleStateConverter
RadTreeView allows binding the check boxes to a custom property from the associated data object by specifying the RadTreeView.CheckedMember property. A common case is when the specified CheckedMember is a property of custom type and it should be converted to ToggleState which is required by the check boxes in the tree view. This article demonstrates how you can modify the way a property is being displayed and edited by using custom TypeConverters. A Type Converter is used to convert values between data types. Here are the four main methods that are usually used when implementing a custom Type Converter.
Override the CanConvertFrom method that specifies which type the converter can convert from.
Override the ConvertFrom method that implements the conversion.
Override the CanConvertTo method that specifies which type the converter can convert to.
Override the ConvertTo method that implements the conversion.
Consider the RadTreeView is populated with Item objects having the following properties:
Item class
The tree view is populated with data as follows:
Bind RadTreeView
The specified CheckedMember is the Item.IsActive property which is typeof(string) indicating the "true" / "false" values. In order to convert these string values to a valid ToggleState you need to use a custom Type Converter. The following code snippet illustrates a sample implementation:
Custom TypeConverter's implementation
Now, you need to apply the custom TypeConverter to the RadTreeView.ToggleStateConverter property:
The property was introduced in R3 2018 (version 2018.3.911).
Set the ToggleStateConverter
Now, you can toggle/untoggle the nodes and this will be properly reflected to the underlying data object:
Note that following this approach it is possible to convert any custom type to ToggleState and thus bind the check boxes in the tree view to any custom property that you have. It is just necessary to implement the specific conversion.