generated from atc-net/atc-template-dotnet-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from atc-net/feature/ObjectNullToVisibilityVi…
…sibleValueConverter-ObjectNotNullToVisibilityCollapsedValueConverter feat: Add ObjectNotNullToVisibilityCollapsedValueConverter and Object…
- Loading branch information
Showing
5 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Atc.Wpf/ValueConverters/ObjectNotNullToVisibilityCollapsedValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Atc.Wpf/ValueConverters/ObjectNullToVisibilityVisibleValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test/Atc.Wpf.Tests/ValueConverters/ObjectNotNullToVisibilityCollapsedValueConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
test/Atc.Wpf.Tests/ValueConverters/ObjectNullToVisibilityVisibleValueConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |