[tasque/viewmodel: 51/78] Add generic ValueConverter



commit 9d3ece358a65e66d57cb4bd8eb7f86c32e519414
Author: Antonius Riha <antoniusriha gmail com>
Date:   Mon Aug 6 22:27:24 2012 +0200

    Add generic ValueConverter
    
    Added an interface and an abstract class.

 src/libtasqueui/IValueConverter.cs |   36 ++++++++++++++++++++++++
 src/libtasqueui/ValueConverter.cs  |   54 ++++++++++++++++++++++++++++++++++++
 src/libtasqueui/libtasqueui.csproj |    2 +
 3 files changed, 92 insertions(+), 0 deletions(-)
---
diff --git a/src/libtasqueui/IValueConverter.cs b/src/libtasqueui/IValueConverter.cs
new file mode 100644
index 0000000..66d1ba0
--- /dev/null
+++ b/src/libtasqueui/IValueConverter.cs
@@ -0,0 +1,36 @@
+// 
+// IValueConverter.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System.Globalization;
+using CollectionTransforms;
+
+namespace Tasque.UIModel
+{
+	public interface IValueConverter<TSource, TTarget> : IValueConverter
+	{
+		TTarget Convert (TSource value, object parameter, CultureInfo culture);
+		TSource ConvertBack (TTarget value, object parameter, CultureInfo culture);
+	}
+}
diff --git a/src/libtasqueui/ValueConverter.cs b/src/libtasqueui/ValueConverter.cs
new file mode 100644
index 0000000..8e404c3
--- /dev/null
+++ b/src/libtasqueui/ValueConverter.cs
@@ -0,0 +1,54 @@
+// 
+// ValueConverter.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using System.Globalization;
+using CollectionTransforms;
+
+namespace Tasque.UIModel
+{
+	public abstract class ValueConverter<TSource, TTarget> : IValueConverter<TSource, TTarget>
+	{
+		public abstract TTarget Convert (TSource value, object parameter, CultureInfo culture);
+
+		public abstract TSource ConvertBack (TTarget value, object parameter, CultureInfo culture);
+		
+		object IValueConverter.Convert (object value, Type targetType, object parameter, CultureInfo culture)
+		{
+			return Convert ((TSource)value, parameter, culture);
+		}
+
+		object IValueConverter.ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
+		{
+			TTarget val;
+			try {
+				val = (TTarget)value;
+			} catch (InvalidCastException) {
+				val = default (TTarget);
+			}
+			return ConvertBack (val, parameter, culture);
+		}
+	}
+}
diff --git a/src/libtasqueui/libtasqueui.csproj b/src/libtasqueui/libtasqueui.csproj
index 21d0fae..0d1b925 100644
--- a/src/libtasqueui/libtasqueui.csproj
+++ b/src/libtasqueui/libtasqueui.csproj
@@ -114,6 +114,8 @@
     </Compile>
     <Compile Include="Legacy\TaskGroupConverter.cs" />
     <Compile Include="Legacy\TaskGroup.cs" />
+    <Compile Include="IValueConverter.cs" />
+    <Compile Include="ValueConverter.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]