Flexible TreeView Flexible TreeView


Support & Community

Data format customization

Previous Table of Contents Next

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 text appearance. To change node control`s output text format, use the DisplayFormat property. To do that, enable the Enabled property and 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 {0} placeholder in your format.
For instance, to display a number with two decimal digits, use this code:

// 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;
// force to show bound numbers 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);


  • Note that your bound data would not be changed when text formatting is enabled and they will appear unformatted in the edit control;
  • You may exclude the data placeholder ({0}) and show a text that`s not bound to your node control.

 

NodeControlFormatValue event

The DisplayFormat node control property is useful when you want to apply a static custom text format. Also, Flexible TreeView provides the possibility to dynamically format the node control`s text. To do that, use the NodeControlFormatValue treeview event as shown below. Note that 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 pTreeview, FormatValueEventArgs pArgs)
{
  // provide the custom formatting only for our node control.
  if (pArgs.NodeControl == num)
  {
    decimal val = (decimal)pArgs.Value;
    if (val > 1024)
    {
      // format the specified value. This text will be displayed in the node control.
      pArgs.FormattedValue = string.Format("{0} Kb.", val/1024);
    }
  }
}

Previous Top Next


Last news
Bookmark and Share
Use Flexible TreeView in WPF project
Sunday, 18 December 2011

Do you have a WPF project and want to use Flexible TreeView there? No problem!

Flexible TreeView v3.4
Friday, 16 December 2011

Flexible TreeView v3.4 maintenance release has been released.

Flexible TreeView v3.3
Friday, 23 September 2011

Flexible TreeView v3.3 maintenance release has been released. Separate assemblies for .NET 2.0, 4.0 and 4.0 Client Profile, HTML markup extension, etc.

Latest release

Version:
Release date:
3.4
Dec 16, 2011


Copyright © 2006-2012 ARMSoft. All rights reserved.