Data format customization
Use these methods to customize the node control text format and appearance.
DisplayFormat#
All text node controls display bound data as text using the ToString
method, but this is not useful in cases when you need to customize the displayed text appearance. To change the node control's output text format, use the DisplayFormat node control property:
- Enable its Enabled property.
- Provide a text format in the FormatText property.
After that, bound data will be displayed using the String.Format
method, so FormatText's format is identical to the String.Format
method's formatting rules. The String.Format
method will be called with one parameter, so to insert a data value into the output text, use the {0}
placeholder in your format.
For instance, to display a number with two decimal digits, use the code example below:
// Custom node class.
class NodeEx : Node
{
public decimal Salary;
}
NodeTextBox tb = new NodeTextBox();
tb.AttachTo(tree);
NodeNumeric num = new NodeNumeric();
num.DataFieldName = "Salary";
num.DisplayFormat.Enabled = true;
// Display the bound number value with two decimal points.
num.DisplayFormat.FormatText = "{0:N2}";
num.AttachTo(tree);
NodeEx node = new NodeEx();
node.Text = "John Smith";
node.Salary = 5000.6786M;
node.AttachTo(tree);
' Custom node class.
Class NodeEx
Inherits Node
Public Salary As Decimal
End Class
Dim tb As New NodeTextBox()
tb.AttachTo(tree)
Dim num As New NodeNumeric()
num.DataFieldName = "Salary"
num.DisplayFormat.Enabled = True
' Display the bound number value with two decimal points.
num.DisplayFormat.FormatText = "{0:N2}"
num.AttachTo(tree)
Dim node As New NodeEx()
node.Text = "John Smith"
node.Salary = 5000.6786D
node.AttachTo(tree)
Note
- Your bound data will not be changed when the text formatting is enabled, so it will appear unformatted in the edit control when the user starts editing this text.
- You can just don't use the provided data placeholder
{0}
and assign any text to the FormatText property so this text will be displayed in the node control as is.
NodeControlFormatValue event#
The DisplayFormat node control property is useful when you want to display the node control's bound data in a specific format.
Also, Flexible TreeView provides the possibility to dynamically format the node control's text. To do that, handle the NodeControlFormatValue treeview event and provide the custom formatting in the event handler.
Note
The NodeControlFormatValue event will be raised only when custom formatting is disabled (DisplayFormat.Enabled=false).
Example
// We have the NodeNumeric node control to display a file size and want to display 'Kb' when the file size is greater than 1024.
NodeNumeric num = new NodeNumeric();
// 1. Disable custom formatting for the node control. Note that it is disabled by default!
num.DisplayFormat.Enabled = false;
// 2. Subscribe to the NodeControlFormatValue treeview event.
tree.NodeControlFormatValue += tree_NodeControlFormatValue;
// 3. Event handler.
void tree_NodeControlFormatValue(FlexibleTreeView treeview, FormatValueEventArgs args)
{
// Provide the custom formatting only for our node control.
if (args.NodeControl == num)
{
decimal val = (decimal)args.Value;
if (val > 1024)
{
// Format the specified value. This text will be displayed in the node control.
args.FormattedValue = string.Format("{0} Kb.", val / 1024);
}
}
}
' We have the NodeNumeric node control to display a file size and want to display 'Kb' when the file size is greater than 1024.
Dim num As New NodeNumeric()
' 1. Disable custom formatting for the node control. Note that it is disabled by default!
num.DisplayFormat.Enabled = False
' 2. Subscribe to the NodeControlFormatValue treeview event.
AddHandler tree.NodeControlFormatValue, AddressOf tree_NodeControlFormatValue
' 3. Event handler.
Private Sub tree_NodeControlFormatValue(treeview As FlexibleTreeView, args As FormatValueEventArgs)
' Provide the custom formatting only for our node control.
If args.NodeControl Is num Then
Dim val As Decimal = CDec(args.Value)
If val > 1024 Then
' Format the specified value. This text will be displayed in the node control.
args.FormattedValue = String.Format("{0} Kb.", val / 1024);
End If
End If
End Sub