Added Format property to ColumnStyle

This commit is contained in:
tznind
2020-12-16 19:47:15 +00:00
parent 7cf34777d3
commit e1b60fb9fa
2 changed files with 13 additions and 3 deletions

View File

@@ -26,6 +26,11 @@ namespace Terminal.Gui.Views {
/// </summary>
public Func<object,string> RepresentationGetter;
/// <summary>
/// Defines the format for values e.g. "yyyy-MM-dd" for dates
/// </summary>
public string Format{get;set;}
/// <summary>
/// Set the maximum width of the column in characters. This value will be ignored if more than the tables <see cref="TableView.MaxCellWidth"/>. Defaults to <see cref="TableView.DefaultMaxCellWidth"/>
/// </summary>
@@ -56,6 +61,13 @@ namespace Terminal.Gui.Views {
/// <returns></returns>
public string GetRepresentation (object value)
{
if(!string.IsNullOrWhiteSpace(Format)) {
if(value is IFormattable f)
return f.ToString(Format,null);
}
if(RepresentationGetter != null)
return RepresentationGetter(value);

View File

@@ -145,9 +145,7 @@ namespace UICatalog.Scenarios {
var negativeRight = new ColumnStyle() {
RepresentationGetter = (v)=> v is double d ?
d.ToString("0.##"):
v.ToString(),
Format = "0.##",
MinWidth = 10,
AlignmentGetter = (v)=>v is double d ?
// align negative values right