Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Activities.Core.Presentation / System / Activities / Core / Presentation / NotConverter.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Core.Presentation
6 {
7     // Code borrowed from System.Activities.Presentation
8
9     using System;
10     using System.ComponentModel;
11     using System.Windows;
12     using System.Windows.Data;
13     using System.Globalization;
14     using System.Reflection;
15     using System.Diagnostics.CodeAnalysis;
16
17     // <summary>
18     // Transform bool value using logical not.
19     // </summary>
20     [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
21     internal sealed class NotConverter : IValueConverter
22     {
23         // IValueConverter Members
24
25         public object Convert(object o, Type targetType, object parameter, CultureInfo culture)
26         {
27             return !(bool)o;
28         }
29
30         public object ConvertBack(object o, Type targetType, object parameter, CultureInfo culture)
31         {
32             return !AssureBool(o, false);
33         }
34
35         static bool AssureBool(object value, bool defaultIfNull)
36         {
37             if (value is bool?)
38             {
39                 bool? nbValue = (bool?)value;
40
41                 if (nbValue.HasValue)
42                 {
43                     return nbValue.Value;
44                 }
45                 else
46                 {
47                     return defaultIfNull;
48                 }
49             }
50
51             return (bool)value;
52         }
53     }
54 }