Skip to content

Commit

Permalink
Merge pull request #124 from atc-net/feature/ObjectNullToVisibilityVi…
Browse files Browse the repository at this point in the history
…sibleValueConverter-ObjectNotNullToVisibilityCollapsedValueConverter

feat: Add ObjectNotNullToVisibilityCollapsedValueConverter and Object…
  • Loading branch information
davidkallesen authored Jun 12, 2024
2 parents 31387ea + ac04b32 commit 7e3f7f9
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Atc.Wpf/ValueConverters/@Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
| Null -> X | NullCheckValueConverter | NULL -> Parameter if set | Not supported |
| Null -> UnsetValue | NullToUnsetValueConverter | NULL -> DependencyProperty.UnsetValue | Object -> DependencyProperty.UnsetValue |
| Object -> Bool | ObjectNotNullToBoolValueConverter | NotNULL -> True | Not supported |
| Object -> Visibility | ObjectNotNullToVisibilityCollapsedValueConverter | NotNULL -> Collapsed | Not supported |
| Object -> Visibility | ObjectNotNullToVisibilityVisibleValueConverter | NotNULL -> Visible | Not supported |
| Object -> Visibility | ObjectNullToVisibilityCollapsedValueConverter | NULL -> Collapsed | Not supported |
| Object -> Visibility | ObjectNullToVisibilityVisibleValueConverter | NULL -> Visible | Not supported |
| Object[] -> Visibility | MultiObjectNullToVisibilityCollapsedValueConverter | All-NULL -> Collapsed | Not supported |
| Object -> Bool | ObjectNullToBoolValueConverter | NULL => True | Not supported |

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Atc.Wpf.ValueConverters;

/// <summary>
/// ValueConverter: Object NotNull To Visibility-Collapsed.
/// </summary>
[ValueConversion(typeof(object), typeof(Visibility), ParameterType = typeof(Visibility))]
public sealed class ObjectNotNullToVisibilityCollapsedValueConverter : IValueConverter
{
public Visibility NonVisibility { get; set; } = Visibility.Visible;

/// <inheritdoc />
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
var nonVisibility = NonVisibility;

if (parameter is Visibility visibility and Visibility.Visible)
{
nonVisibility = visibility;
}

return value is null
? nonVisibility
: Visibility.Collapsed;
}

/// <inheritdoc />
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotSupportedException("This is a OneWay converter.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Atc.Wpf.ValueConverters;

/// <summary>
/// ValueConverter: Object Null To Visibility-Visible.
/// </summary>
[ValueConversion(typeof(object), typeof(Visibility))]
public sealed class ObjectNullToVisibilityVisibleValueConverter : IValueConverter
{
/// <inheritdoc />
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value is null
? Visibility.Visible
: Visibility.Collapsed;
}

/// <inheritdoc />
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotSupportedException("This is a OneWay converter.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Atc.Wpf.Tests.ValueConverters;

public class ObjectNotNullToVisibilityCollapsedValueConverterTests
{
private readonly IValueConverter converter = new ObjectNotNullToVisibilityCollapsedValueConverter();

[Theory]
[InlineData(Visibility.Visible, null)]
[InlineData(Visibility.Collapsed, true)]
[InlineData(Visibility.Collapsed, "Hello")]
public void Convert(Visibility expected, object? input)
=> Assert.Equal(
expected,
converter.Convert(input, targetType: null, parameter: null, culture: null));

[Theory]
[InlineData(Visibility.Visible, null, null)]
[InlineData(Visibility.Collapsed, true, null)]
[InlineData(Visibility.Collapsed, "Hello", null)]
[InlineData(Visibility.Visible, null, Visibility.Collapsed)]
[InlineData(Visibility.Collapsed, true, Visibility.Collapsed)]
[InlineData(Visibility.Collapsed, "Hello", Visibility.Collapsed)]
[InlineData(Visibility.Visible, null, Visibility.Hidden)]
[InlineData(Visibility.Collapsed, true, Visibility.Hidden)]
[InlineData(Visibility.Collapsed, "Hello", Visibility.Hidden)]
[InlineData(Visibility.Visible, null, Visibility.Visible)]
[InlineData(Visibility.Collapsed, true, Visibility.Visible)]
[InlineData(Visibility.Collapsed, "Hello", Visibility.Visible)]
public void Convert_Parameter(Visibility expected, object? input, object? parameter)
=> Assert.Equal(
expected,
converter.Convert(input, targetType: null, parameter, culture: null));

[Fact]
public void ConvertBack_Should_Throw_Exception()
{
// Act
var exception = Record.Exception(() => converter.ConvertBack(value: null, targetType: null, parameter: null, culture: null));

// Assert
Assert.IsType<NotSupportedException>(exception);
Assert.Equal("This is a OneWay converter.", exception.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Atc.Wpf.Tests.ValueConverters;

public class ObjectNullToVisibilityVisibleValueConverterTests
{
private readonly IValueConverter converter = new ObjectNullToVisibilityVisibleValueConverter();

[Theory]
[InlineData(Visibility.Visible, null)]
[InlineData(Visibility.Collapsed, true)]
[InlineData(Visibility.Collapsed, "Hello")]
public void Convert(Visibility expected, object? input)
=> Assert.Equal(
expected,
converter.Convert(input, targetType: null, parameter: null, culture: null));

[Fact]
public void ConvertBack_Should_Throw_Exception()
{
// Act
var exception = Record.Exception(() => converter.ConvertBack(value: null, targetType: null, parameter: null, culture: null));

// Assert
Assert.IsType<NotSupportedException>(exception);
Assert.Equal("This is a OneWay converter.", exception.Message);
}
}

0 comments on commit 7e3f7f9

Please sign in to comment.