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