Skip to content

Commit 19a8b31

Browse files
Merge pull request #100 from atc-net/feature/Support-StringCaseFormat-in-EnumDescriptionToStringValueConverter
Feature/support string case format in enum description to string value converter
2 parents d6a7674 + c952b01 commit 19a8b31

File tree

10 files changed

+98
-21
lines changed

10 files changed

+98
-21
lines changed

sample/Atc.Wpf.Sample/Atc.Wpf.Sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</ItemGroup>
3333

3434
<ItemGroup>
35-
<PackageReference Include="Atc" Version="2.0.395" />
35+
<PackageReference Include="Atc" Version="2.0.422" />
3636
<PackageReference Include="ControlzEx" Version="6.0.0" />
3737
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
3838
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />

src/Atc.Wpf.Controls/Atc.Wpf.Controls.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="Atc" Version="2.0.395" />
25+
<PackageReference Include="Atc" Version="2.0.422" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

src/Atc.Wpf.FontIcons/Atc.Wpf.FontIcons.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="Atc" Version="2.0.395" />
23+
<PackageReference Include="Atc" Version="2.0.422" />
2424
</ItemGroup>
2525

2626
<ItemGroup>

src/Atc.Wpf/Atc.Wpf.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@
10071007
</ItemGroup>
10081008

10091009
<ItemGroup>
1010-
<PackageReference Include="Atc" Version="2.0.395" />
1010+
<PackageReference Include="Atc" Version="2.0.422" />
10111011
</ItemGroup>
10121012

10131013
<ItemGroup>

src/Atc.Wpf/ValueConverters/EnumDescriptionToStringValueConverter.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
1+
// ReSharper disable InvertIf
12
namespace Atc.Wpf.ValueConverters;
23

34
/// <summary>
45
/// ValueConverter: Enum-Description To String.
56
/// </summary>
67
public sealed class EnumDescriptionToStringValueConverter : IValueConverter
78
{
8-
/// <inheritdoc />
9+
/// <summary>
10+
/// Converts a value.
11+
/// Supported case-formats:
12+
/// <list type="table">
13+
/// <item>
14+
/// <term>U</term>
15+
/// <description>Converts the entire string to uppercase.</description>
16+
/// </item>
17+
/// <item>
18+
/// <term>u</term>
19+
/// <description>Capitalizes the first character of the string.</description>
20+
/// </item>
21+
/// <item>
22+
/// <term>L</term>
23+
/// <description>Converts the entire string to lowercase.</description>
24+
/// </item>
25+
/// <item>
26+
/// <term>l</term>
27+
/// <description>Converts the first character of the string to lowercase.</description>
28+
/// </item>
29+
/// <item>
30+
/// <term>Ul</term>
31+
/// <description>Converts the string to uppercase with the first character in lowercase.</description>
32+
/// </item>
33+
/// <item>
34+
/// <term>Lu</term>
35+
/// <description>Converts the string to lowercase with the first character capitalized.</description>
36+
/// </item>
37+
/// <item>
38+
/// <term>[Format]:</term>
39+
/// <description>Applies the specified format and appends a colon ´<b>:</b>´ to the end.</description>
40+
/// </item>
41+
/// <item>
42+
/// <term>[Format].</term>
43+
/// <description>Applies the specified format and appends a period ´<b>.</b>´ to the end.</description>
44+
/// </item>
45+
/// </list>
46+
/// </summary>
47+
/// <param name="value">The value produced by the binding source - Must be an Enum value.</param>
48+
/// <param name="targetType">The type of the binding target property - Not used.</param>
49+
/// <param name="parameter">The converter parameter to use - Optional case-formatter.</param>
50+
/// <param name="culture">The culture to use in the converter - Not used.</param>
51+
/// <returns>
52+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
53+
/// </returns>
954
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
1055
{
1156
ArgumentNullException.ThrowIfNull(value);
@@ -15,6 +60,19 @@ public object Convert(object? value, Type targetType, object? parameter, Culture
1560
throw new UnexpectedTypeException($"Type {value.GetType().FullName} is not an enum type");
1661
}
1762

63+
if (parameter is not null)
64+
{
65+
var parameterValue = parameter.ToString();
66+
if (!string.IsNullOrEmpty(parameterValue))
67+
{
68+
var caseFormatter = "{0:" + parameterValue + "}";
69+
return string.Format(
70+
StringCaseFormatter.Default,
71+
caseFormatter,
72+
EnumHelper.GetDescription(enumValue));
73+
}
74+
}
75+
1876
return EnumHelper.GetDescription(enumValue);
1977
}
2078

test/Atc.Wpf.Controls.Tests/Atc.Wpf.Controls.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Atc" Version="2.0.395" />
11-
<PackageReference Include="Atc.XUnit" Version="2.0.395" />
10+
<PackageReference Include="Atc" Version="2.0.422" />
11+
<PackageReference Include="Atc.XUnit" Version="2.0.422" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
13-
<PackageReference Include="xunit" Version="2.6.3" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
13+
<PackageReference Include="xunit" Version="2.6.6" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>

test/Atc.Wpf.Tests/Atc.Wpf.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Atc" Version="2.0.395" />
11-
<PackageReference Include="Atc.XUnit" Version="2.0.395" />
10+
<PackageReference Include="Atc" Version="2.0.422" />
11+
<PackageReference Include="Atc.XUnit" Version="2.0.422" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
13-
<PackageReference Include="xunit" Version="2.6.3" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
13+
<PackageReference Include="xunit" Version="2.6.6" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>

test/Atc.Wpf.Tests/ValueConverters/EnumDescriptionToStringValueConverterTests.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1+
// ReSharper disable StringLiteralTypo
12
namespace Atc.Wpf.Tests.ValueConverters;
23

34
public class EnumDescriptionToStringValueConverterTests
45
{
56
private readonly IValueConverter converter = new EnumDescriptionToStringValueConverter();
67

78
[Theory]
8-
[InlineData("Monday", DayOfWeek.Monday)]
9-
public void Convert(string expected, DayOfWeek input)
9+
[InlineData("Monday", DayOfWeek.Monday, null)]
10+
[InlineData("MONDAY", DayOfWeek.Monday, "U")]
11+
[InlineData("monday", DayOfWeek.Monday, "L")]
12+
[InlineData("Monday", DayOfWeek.Monday, "u")]
13+
[InlineData("monday", DayOfWeek.Monday, "l")]
14+
[InlineData("mONDAY", DayOfWeek.Monday, "Ul")]
15+
[InlineData("Monday", DayOfWeek.Monday, "Lu")]
16+
[InlineData("MONDAY.", DayOfWeek.Monday, "U.")]
17+
[InlineData("monday.", DayOfWeek.Monday, "L.")]
18+
[InlineData("Monday.", DayOfWeek.Monday, "u.")]
19+
[InlineData("monday.", DayOfWeek.Monday, "l.")]
20+
[InlineData("mONDAY.", DayOfWeek.Monday, "Ul.")]
21+
[InlineData("Monday.", DayOfWeek.Monday, "Lu.")]
22+
[InlineData("MONDAY:", DayOfWeek.Monday, "U:")]
23+
[InlineData("monday:", DayOfWeek.Monday, "L:")]
24+
[InlineData("Monday:", DayOfWeek.Monday, "u:")]
25+
[InlineData("monday:", DayOfWeek.Monday, "l:")]
26+
[InlineData("mONDAY:", DayOfWeek.Monday, "Ul:")]
27+
[InlineData("Monday:", DayOfWeek.Monday, "Lu:")]
28+
public void Convert(string expected, DayOfWeek input, string? inputParameter)
1029
=> Assert.Equal(
1130
expected,
12-
converter.Convert(input, targetType: null, parameter: null, culture: null));
31+
converter.Convert(input, targetType: null, inputParameter, culture: null));
1332

1433
[Fact]
1534
public void ConvertBack_Should_Throw_Exception()

test/Atc.Wpf.Theming.Tests/Atc.Wpf.Theming.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Atc" Version="2.0.395" />
11-
<PackageReference Include="Atc.XUnit" Version="2.0.395" />
10+
<PackageReference Include="Atc" Version="2.0.422" />
11+
<PackageReference Include="Atc.XUnit" Version="2.0.422" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
13-
<PackageReference Include="xunit" Version="2.6.3" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
13+
<PackageReference Include="xunit" Version="2.6.6" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>

tool/Atc.Wpf.Generator.FontIconResources/Atc.Wpf.Generator.FontIconResources.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Atc" Version="2.0.395" />
11+
<PackageReference Include="Atc" Version="2.0.422" />
1212
<PackageReference Include="ControlzEx" Version="6.0.0" />
1313
<PackageReference Include="CssParser" Version="1.3.0">
1414
<NoWarn>NU1701</NoWarn>

0 commit comments

Comments
 (0)