diff MetroWpf/MetroWpf.Xaml/Converters/DoubleToGridLengthConverter.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Xaml/Converters/DoubleToGridLengthConverter.cs	Mon Mar 12 23:05:21 2012 +0800
@@ -0,0 +1,52 @@
+using System;
+using System.Globalization;
+using System.Windows;
+using System.Windows.Data;
+
+namespace MetroWpf.Xaml.Converters
+{
+    [ValueConversion(typeof(double), typeof(GridLength))]
+    public sealed class DoubleToGridLengthConverter : IValueConverter
+    {
+        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            if (!(value is double))
+            {
+                return new GridLength(0.0, GridUnitType.Auto);
+            }
+
+            var unitType = parameter as string;
+
+            switch (unitType)
+            {
+                case "Auto":
+                    return new GridLength(0.0, GridUnitType.Auto);
+                case "*":
+                    return new GridLength((double)value, GridUnitType.Star);
+                default:
+                    return new GridLength((double)value, GridUnitType.Pixel);
+            }
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+        {
+            var unitType = parameter as string;
+
+            if (!(value is GridLength))
+            {
+                return DependencyProperty.UnsetValue;
+            }
+
+            var length = (GridLength)value;
+            switch (unitType)
+            {
+                case "Auto":
+                    return length.IsAuto ? length.Value : DependencyProperty.UnsetValue;
+                case "*":
+                    return length.IsStar ? length.Value : DependencyProperty.UnsetValue;
+                default:
+                    return length.IsAbsolute ? length.Value : DependencyProperty.UnsetValue;
+            }
+        }
+    }
+} ;
\ No newline at end of file