Ver código fonte

Added Xamarin IntToColorConverter

Frank van den Bos 1 ano atrás
pai
commit
fcfcd9adbf

+ 38 - 0
InABox.Mobile/InABox.Mobile.Shared/Converters/IntToColorConverter.cs

@@ -0,0 +1,38 @@
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using Xamarin.Forms;
+
+namespace InABox.Mobile
+{
+    public class IntToColorConverterThreshold
+    {
+        public int Lower { get; set; }
+        public int Upper { get; set; }
+        public Color Color { get; set; }
+    }
+    
+    public class IntToColorConverter: UtilityConverter<int,Color>
+    {
+        
+        public IList<IntToColorConverterThreshold> Thresholds { get; private set; }
+        
+        public Color DefaultColor { get; set; }
+
+        public IntToColorConverter()
+        {
+            Thresholds = new ObservableCollection<IntToColorConverterThreshold>();
+        }
+        
+        protected override Color Convert(int value)
+        {
+            foreach (var threshold in Thresholds.OrderBy(x=>x.Lower))
+            {
+                if (value >= threshold.Lower && value <= threshold.Upper)
+                    return threshold.Color;
+
+            }
+            return DefaultColor;
+        }
+    }
+}