Menu

No custom formatter for NodeTextBox?

YourGod
2017-02-07
2017-02-07
  • YourGod

    YourGod - 2017-02-07

    If a column is bound to a numeric value property, you may want to format it, like adding thousand separators or make it empty. For example, look at the "Folder Browser" in the sample application. Folders do not have size, so ideally, the size colume should be empty. Yet, in the sample, a "0" is displayed. For files, it just displays the raw number like "483854828348". This should look a lot better if it was "4,838 MB".

    One simple solution could be just creating a string property, and set the formatted value there, and bind the colume to it. But that way, the colume does not have actual values, so sorting will not work out of the box.

    I browsed the source code for a possible way to set a formatter, but failed. If there is no such thing, please add it. Something like,

    NodeTextBox.SetFormatter(MyFormatter);
    
    string MyFormatter(TreeNodeAdv node, object value)
    {
        if(node.IsLeaf)
        {
            long size = (long)value;
            return GetSizeString(size);
        }
        else
        {
            return String.Empty;
        }
    }
    
     
  • YourGod

    YourGod - 2017-02-07

    I am not sure if these are the optimal way, but after investigation, I have achieved the result using,

    (1)Do not display for folder

    NodeTextBox.IsVisibleValueNeeded += DoNotShowFolder;
    
        private void DoNotShowFolder(object sender, NodeControlValueEventArgs e)
        {
            e.Value = e.Node.IsLeaf;
        }
    

    (2) Format number

    NodeTextBox.DrawText += FormatNumber;
    
         private void FormatNumber(object sender, DrawEventArgs e)
        {
            if (e.Node.IsLeaf)
            {
                var size = (e.Node.Tag as MyNode).Size;
                e.Text = FormatSize(size);
            }
        }
    
     

Log in to post a comment.