forked from AvaloniaUI/Avalonia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogicalNotNode.cs
More file actions
75 lines (67 loc) · 2.43 KB
/
Copy pathLogicalNotNode.cs
File metadata and controls
75 lines (67 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Globalization;
namespace Avalonia.Data.Core
{
public class LogicalNotNode : ExpressionNode, ITransformNode
{
public override string Description => "!";
protected override void NextValueChanged(object value)
{
base.NextValueChanged(Negate(value));
}
private static object Negate(object v)
{
if (v != AvaloniaProperty.UnsetValue)
{
var s = v as string;
if (s != null)
{
bool result;
if (bool.TryParse(s, out result))
{
return !result;
}
else
{
return new BindingNotification(
new InvalidCastException($"Unable to convert '{s}' to bool."),
BindingErrorType.Error);
}
}
else
{
try
{
var boolean = Convert.ToBoolean(v, CultureInfo.InvariantCulture);
return !boolean;
}
catch (InvalidCastException)
{
// The error message here is "Unable to cast object of type 'System.Object'
// to type 'System.IConvertible'" which is kinda useless so provide our own.
return new BindingNotification(
new InvalidCastException($"Unable to convert '{v}' to bool."),
BindingErrorType.Error);
}
catch (Exception e)
{
return new BindingNotification(e, BindingErrorType.Error);
}
}
}
return AvaloniaProperty.UnsetValue;
}
public object Transform(object value)
{
var originalType = value.GetType();
var negated = Negate(value);
if (negated is BindingNotification)
{
return negated;
}
return Convert.ChangeType(negated, originalType);
}
}
}