/
BooleanToVisibilityTypeConverter.cs
66 lines (58 loc) · 2.06 KB
/
BooleanToVisibilityTypeConverter.cs
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
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
#if NETFX_CORE || HAS_UNO
using Windows.UI.Xaml;
#else
using System.Windows;
#endif
#if HAS_UNO
namespace ReactiveUI.Uno
#else
namespace ReactiveUI
#endif
{
/// <summary>
/// This type convert converts between Boolean and XAML Visibility - the
/// conversionHint is a BooleanToVisibilityHint.
/// </summary>
public class BooleanToVisibilityTypeConverter : IBindingTypeConverter
{
/// <inheritdoc/>
public int GetAffinityForObjects(Type fromType, Type toType)
{
if (fromType == typeof(bool) && toType == typeof(Visibility))
{
return 10;
}
if (fromType == typeof(Visibility) && toType == typeof(bool))
{
return 10;
}
return 0;
}
/// <inheritdoc/>
public bool TryConvert(object from, Type toType, object conversionHint, out object result)
{
var hint = conversionHint is BooleanToVisibilityHint ?
(BooleanToVisibilityHint)conversionHint :
BooleanToVisibilityHint.None;
if (toType == typeof(Visibility))
{
var fromAsBool = hint.HasFlag(BooleanToVisibilityHint.Inverse) ? !(bool)@from : (bool)from;
#if !NETFX_CORE && !HAS_UNO
var notVisible = hint.HasFlag(BooleanToVisibilityHint.UseHidden) ? Visibility.Hidden : Visibility.Collapsed;
#else
var notVisible = Visibility.Collapsed;
#endif
result = fromAsBool ? Visibility.Visible : notVisible;
return true;
}
var fromAsVis = (Visibility)from;
result = fromAsVis == Visibility.Visible ^ !hint.HasFlag(BooleanToVisibilityHint.Inverse);
return true;
}
}
}