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