comparison MetroWpf/MetroWpf.Xaml/Converters/ThicknessToDoubleConverter.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(Thickness), typeof(double))]
9 public sealed class ThicknessToDoubleConverter : IValueConverter
10 {
11 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12 {
13 if (!(value is Thickness))
14 {
15 return DependencyProperty.UnsetValue;
16 }
17
18 var thickness = (Thickness)value;
19
20 var propertyName = parameter as string;
21 switch (propertyName)
22 {
23 case "Left":
24 return thickness.Left;
25 case "Top":
26 return thickness.Top;
27 case "Right":
28 return thickness.Right;
29 case "Bottom":
30 return thickness.Bottom;
31 default:
32 return (thickness.Left + thickness.Top + thickness.Bottom + thickness.Right) / 4;
33 }
34 }
35
36 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
37 {
38 if (!(value is double))
39 {
40 return DependencyProperty.UnsetValue;
41 }
42
43 var parameters = parameter as string;
44 if (parameters != null)
45 {
46 var propertyNames = parameters.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
47 if (propertyNames.Length > 0)
48 {
49 var thickness = new Thickness(0);
50
51 var changed = false;
52
53 foreach (var propertyName in propertyNames)
54 {
55 switch (propertyName)
56 {
57 case "Left":
58 thickness.Left = (double)value;
59 changed = true;
60 break;
61 case "Top":
62 thickness.Top = (double)value;
63 changed = true;
64 break;
65 case "Right":
66 thickness.Right = (double)value;
67 changed = true;
68 break;
69 case "Bottom":
70 thickness.Bottom = (double)value;
71 changed = true;
72 break;
73 }
74 }
75
76 if (changed)
77 {
78 return thickness;
79 }
80 }
81 }
82
83 return new Thickness((double)value);
84 }
85 }
86 } ;