PageRenderTime 28ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/CBR/CBR/Components/Converters/BoolToWindowStateConverter.cs

#
C# | 83 lines | 51 code | 6 blank | 26 comment | 11 complexity | 3cef3ac57948e78a2ac205e98b2a63cc MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Windows.Data;
 6
 7namespace CBR.Components.Converters
 8{
 9    /// <summary>
10	/// Converter : Boolean to System.Windows.Visibility and not revert
11	/// </summary>
12	[ValueConversion(typeof(bool), typeof(System.Windows.WindowState))]
13	public class BoolToWindowStateConverter : IValueConverter
14	{
15		/// <summary>
16		/// Singleton access
17		/// </summary>
18		public static readonly BoolToWindowStateConverter Instance = new BoolToWindowStateConverter(); 
19
20		/// <summary>
21		/// Private constructor for singleton pattern
22		/// </summary>
23		private BoolToWindowStateConverter()
24		{
25		}
26
27		/// <summary>
28		/// IValueConverter.Convert
29		/// </summary>
30		/// <param name="value"></param>
31		/// <param name="targetType"></param>
32		/// <param name="parameter"></param>
33		/// <param name="culture"></param>
34		/// <returns></returns>
35		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
36		{
37			bool val = false;
38			bool param = true;
39
40			if (value != null)
41				val = (bool)value;
42			if (parameter != null)
43				param = System.Convert.ToBoolean(parameter);
44			
45			if (val)
46			{
47				// allow to test on !value, that is not possible in the binding
48				if( param == true )
49                    return System.Windows.WindowState.Maximized;
50				else
51                    return System.Windows.WindowState.Normal;
52			}
53			else
54			{
55				if (param == true)
56                    return System.Windows.WindowState.Normal;
57				else
58                    return System.Windows.WindowState.Maximized;
59			}
60		}
61
62		/// <summary>
63		/// IValueConverter.ConvertBack
64		/// </summary>
65		/// <param name="value"></param>
66		/// <param name="targetType"></param>
67		/// <param name="parameter"></param>
68		/// <param name="culture"></param>
69		/// <returns></returns>
70		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
71		{
72            System.Windows.WindowState val = (System.Windows.WindowState)value;
73            if (val == System.Windows.WindowState.Maximized)
74			{
75				return true;
76			}
77			else
78			{
79				return false;
80			}
81		}
82	}
83}