comparison MetroWpf/MetroWpf.Xaml/Converters/ThicknessPositiveToNegativeConverter.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(Thickness))]
9 public class ThicknessPositiveToNegativeConverter : 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 source = (Thickness)value;
19
20 var parameters = parameter as string;
21 if (parameters != null)
22 {
23 var propertyNames = parameters.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
24 if (propertyNames.Length > 0)
25 {
26 var thickness = new Thickness(0);
27
28 var changed = false;
29
30 foreach (var propertyName in propertyNames)
31 {
32 switch (propertyName)
33 {
34 case "Left":
35 thickness.Left = -source.Left;
36 changed = true;
37 break;
38 case "Top":
39 thickness.Top = -source.Top;
40 changed = true;
41 break;
42 case "Right":
43 thickness.Right = -source.Right;
44 changed = true;
45 break;
46 case "Bottom":
47 thickness.Bottom = -source.Bottom;
48 changed = true;
49 break;
50 }
51 }
52
53 if (changed)
54 {
55 return thickness;
56 }
57 }
58 }
59
60 return source;
61 }
62
63 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
64 {
65 return Convert(value, targetType, parameter, culture);
66 }
67 }
68 } ;