comparison 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
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 using System;
2 using System.Globalization;
3 using System.Windows;
4 using System.Windows.Data;
5
6 namespace MetroWpf.Xaml.Converters
7 {
8 [ValueConversion(typeof(double), typeof(GridLength))]
9 public sealed class DoubleToGridLengthConverter : IValueConverter
10 {
11 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12 {
13 if (!(value is double))
14 {
15 return new GridLength(0.0, GridUnitType.Auto);
16 }
17
18 var unitType = parameter as string;
19
20 switch (unitType)
21 {
22 case "Auto":
23 return new GridLength(0.0, GridUnitType.Auto);
24 case "*":
25 return new GridLength((double)value, GridUnitType.Star);
26 default:
27 return new GridLength((double)value, GridUnitType.Pixel);
28 }
29 }
30
31 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
32 {
33 var unitType = parameter as string;
34
35 if (!(value is GridLength))
36 {
37 return DependencyProperty.UnsetValue;
38 }
39
40 var length = (GridLength)value;
41 switch (unitType)
42 {
43 case "Auto":
44 return length.IsAuto ? length.Value : DependencyProperty.UnsetValue;
45 case "*":
46 return length.IsStar ? length.Value : DependencyProperty.UnsetValue;
47 default:
48 return length.IsAbsolute ? length.Value : DependencyProperty.UnsetValue;
49 }
50 }
51 }
52 } ;