Bläddra i källkod

Added Format property to ColumnStyle

tznind 4 år sedan
förälder
incheckning
e1b60fb9fa
2 ändrade filer med 13 tillägg och 3 borttagningar
  1. 12 0
      Terminal.Gui/Views/TableView.cs
  2. 1 3
      UICatalog/Scenarios/TableEditor.cs

+ 12 - 0
Terminal.Gui/Views/TableView.cs

@@ -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);
 

+ 1 - 3
UICatalog/Scenarios/TableEditor.cs

@@ -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