diff MetroWpf/MetroWpf.Xaml/Converters/EnumBooleanConverter.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Xaml/Converters/EnumBooleanConverter.cs	Mon Mar 12 23:05:21 2012 +0800
@@ -0,0 +1,35 @@
+//http://stackoverflow.com/questions/397556/wpf-how-to-bind-radiobuttons-to-an-enum
+
+using System;
+using System.Windows;
+using System.Windows.Data;
+
+namespace MetroWpf.Xaml.Converters
+{
+  [ValueConversion(typeof(Enum), typeof(Boolean))]
+  public class EnumBooleanConverter : IValueConverter
+  {
+    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
+    {
+      var parameterString = parameter as string;
+      if (parameterString == null)
+        return DependencyProperty.UnsetValue;
+
+      if (Enum.IsDefined(value.GetType(), value) == false)
+        return DependencyProperty.UnsetValue;
+
+      object parameterValue = Enum.Parse(value.GetType(), parameterString);
+
+      return parameterValue.Equals(value);
+    }
+
+    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
+    {
+      var parameterString = parameter as string;
+      if (parameterString == null)
+        return DependencyProperty.UnsetValue;
+
+      return Enum.Parse(targetType, parameterString);
+    }
+  }
+}
\ No newline at end of file