Skip to content

Commit 74a2f45

Browse files
author
Wayfarer
committed
New project added for WPF converters
CommonControls.Converter integrated as a new project and four WPF converters (BooleanToVisibility, EnumToBoolean, NotNullToBoolean, NullToVisibility) implemented. Project files and solution supplemented with author information, imaging reference, and configurations. Minor corrections made in DeleteValueCommand and Lowering.
1 parent 510e04b commit 74a2f45

9 files changed

Lines changed: 245 additions & 5 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: CommonControls.Converter
4+
* FILE: BooleanToVisibilityConverter.cs
5+
* PURPOSE: Boolean to Visibility converter.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System;
10+
using System.Globalization;
11+
using System.Windows;
12+
using System.Windows.Data;
13+
14+
namespace CommonControls.Converter
15+
{
16+
/// <summary>
17+
/// Boolean to Visibility converter.
18+
/// </summary>
19+
/// <seealso cref="System.Windows.Data.IValueConverter" />
20+
public class BooleanToVisibilityConverter : IValueConverter
21+
{
22+
/// <summary>
23+
/// Gets or sets a value indicating whether this <see cref="BooleanToVisibilityConverter"/> is collapse.
24+
/// </summary>
25+
/// <value>
26+
/// <c>true</c> if collapse; otherwise, <c>false</c>.
27+
/// </value>
28+
public bool Collapse { get; set; } = false; // true → Collapsed, false → Hidden
29+
30+
/// <summary>
31+
/// Converts a value.
32+
/// </summary>
33+
/// <param name="value">The value produced by the binding source.</param>
34+
/// <param name="targetType">The type of the binding target property.</param>
35+
/// <param name="parameter">The converter parameter to use.</param>
36+
/// <param name="culture">The culture to use in the converter.</param>
37+
/// <returns>
38+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
39+
/// </returns>
40+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
41+
{
42+
if (value is bool b)
43+
return b ? Visibility.Visible : (Collapse ? Visibility.Collapsed : Visibility.Hidden);
44+
return Visibility.Hidden;
45+
}
46+
47+
/// <summary>
48+
/// Converts a value.
49+
/// </summary>
50+
/// <param name="value">The value that is produced by the binding target.</param>
51+
/// <param name="targetType">The type to convert to.</param>
52+
/// <param name="parameter">The converter parameter to use.</param>
53+
/// <param name="culture">The culture to use in the converter.</param>
54+
/// <returns>
55+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
56+
/// </returns>
57+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
58+
=> value is Visibility v && v == Visibility.Visible;
59+
}
60+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0-windows</TargetFramework>
5+
<UseWPF>true</UseWPF>
6+
<Authors>Wayfarer</Authors>
7+
<Copyright>Lone Wanderer Productions</Copyright>
8+
<Company>Lone Wanderer Productions</Company>
9+
<Nullable>enable</Nullable>
10+
</PropertyGroup>
11+
12+
13+
</Project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: CommonControls.Converter
4+
* FILE: EnumToBooleanConverter.cs
5+
* PURPOSE: Enum to Boolean converter.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System;
10+
using System.Globalization;
11+
using System.Windows.Data;
12+
13+
namespace CommonControls.Converter
14+
{
15+
/// <summary>
16+
/// Converts between an <see cref="Enum"/> value and a <see cref="bool"/> for use in XAML bindings.
17+
/// Typically used to bind enum values to radio buttons or toggle controls:
18+
/// - <see cref="Convert"/> returns true if the enum value matches the converter parameter.
19+
/// - <see cref="ConvertBack"/> returns the enum value represented by the parameter when the boolean is true.
20+
/// </summary>
21+
/// <seealso cref="System.Windows.Data.IValueConverter" />
22+
public class EnumToBooleanConverter : IValueConverter
23+
{
24+
/// <summary>
25+
/// Converts a value.
26+
/// </summary>
27+
/// <param name="value">The value produced by the binding source.</param>
28+
/// <param name="targetType">The type of the binding target property.</param>
29+
/// <param name="parameter">The converter parameter to use.</param>
30+
/// <param name="culture">The culture to use in the converter.</param>
31+
/// <returns>
32+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
33+
/// </returns>
34+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
35+
{
36+
return value?.ToString() == parameter?.ToString();
37+
}
38+
39+
/// <summary>
40+
/// Converts a value.
41+
/// </summary>
42+
/// <param name="value">The value that is produced by the binding target.</param>
43+
/// <param name="targetType">The type to convert to.</param>
44+
/// <param name="parameter">The converter parameter to use.</param>
45+
/// <param name="culture">The culture to use in the converter.</param>
46+
/// <returns>
47+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
48+
/// </returns>
49+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
50+
{
51+
if ((bool)value)
52+
return Enum.Parse(targetType, parameter.ToString()!);
53+
return Binding.DoNothing;
54+
}
55+
}
56+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: SlimControls
4+
* FILE: NotNullToBooleanConverter.cs
5+
* PURPOSE: NotNull to Boolean converter.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System;
10+
using System.Globalization;
11+
using System.Windows.Data;
12+
13+
namespace CommonControls.Converter
14+
{
15+
/// <summary>
16+
/// Converts between a not-null value and a boolean.
17+
/// </summary>
18+
/// <seealso cref="System.Windows.Data.IValueConverter" />
19+
public class NotNullToBooleanConverter : IValueConverter
20+
{
21+
/// <summary>
22+
/// Converts a value.
23+
/// </summary>
24+
/// <param name="value">The value produced by the binding source.</param>
25+
/// <param name="targetType">The type of the binding target property.</param>
26+
/// <param name="parameter">The converter parameter to use.</param>
27+
/// <param name="culture">The culture to use in the converter.</param>
28+
/// <returns>
29+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
30+
/// </returns>
31+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
32+
=> value != null;
33+
34+
/// <summary>
35+
/// Converts a value.
36+
/// </summary>
37+
/// <param name="value">The value that is produced by the binding target.</param>
38+
/// <param name="targetType">The type to convert to.</param>
39+
/// <param name="parameter">The converter parameter to use.</param>
40+
/// <param name="culture">The culture to use in the converter.</param>
41+
/// <returns>
42+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
43+
/// </returns>
44+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
45+
=> Binding.DoNothing;
46+
}
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: CommonControls.Converter
4+
* FILE: NullToVisibilityConverter.cs
5+
* PURPOSE: Convert null to Visibility converter.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System;
10+
using System.Globalization;
11+
using System.Windows;
12+
using System.Windows.Data;
13+
14+
namespace CommonControls.Converter
15+
{
16+
/// <summary>
17+
/// Convert null to Visibility converter.
18+
/// </summary>
19+
/// <seealso cref="System.Windows.Data.IValueConverter" />
20+
public class NullToVisibilityConverter : IValueConverter
21+
{
22+
/// <summary>
23+
/// Gets or sets a value indicating whether this <see cref="NullToVisibilityConverter"/> is collapse.
24+
/// </summary>
25+
/// <value>
26+
/// <c>true</c> if collapse; otherwise, <c>false</c>.
27+
/// </value>
28+
public bool Collapse { get; set; } = true;
29+
30+
/// <summary>
31+
/// Converts a value.
32+
/// </summary>
33+
/// <param name="value">The value produced by the binding source.</param>
34+
/// <param name="targetType">The type of the binding target property.</param>
35+
/// <param name="parameter">The converter parameter to use.</param>
36+
/// <param name="culture">The culture to use in the converter.</param>
37+
/// <returns>
38+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
39+
/// </returns>
40+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
41+
=> value == null ? (Collapse ? Visibility.Collapsed : Visibility.Hidden) : Visibility.Visible;
42+
43+
/// <summary>
44+
/// Converts a value.
45+
/// </summary>
46+
/// <param name="value">The value that is produced by the binding target.</param>
47+
/// <param name="targetType">The type to convert to.</param>
48+
/// <param name="parameter">The converter parameter to use.</param>
49+
/// <param name="culture">The culture to use in the converter.</param>
50+
/// <returns>
51+
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
52+
/// </returns>
53+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
54+
=> Binding.DoNothing;
55+
}
56+
}

CommonControls.Images/CommonControls.Images.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
<PropertyGroup>
44
<TargetFramework>net9.0-windows</TargetFramework>
5-
<Nullable>enable</Nullable>
65
<UseWPF>true</UseWPF>
7-
<ImplicitUsings>enable</ImplicitUsings>
6+
<Authors>Wayfarer</Authors>
7+
<Copyright>Lone Wanderer Productions</Copyright>
8+
<Company>Lone Wanderer Productions</Company>
9+
<Nullable>enable</Nullable>
810
</PropertyGroup>
911

12+
1013
<ItemGroup>
1114
<ProjectReference Include="..\Imaging\Imaging.csproj" />
1215
</ItemGroup>

CoreLibrary.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunicationTests", "Commu
9595
EndProject
9696
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonControls.Images", "CommonControls.Images\CommonControls.Images.csproj", "{74CE25AC-D9ED-44FB-8111-0D10D094D4CA}"
9797
EndProject
98+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonControls.Converter", "CommonControls.Converter\CommonControls.Converter.csproj", "{A2C8D860-41F8-40B8-B939-BCA253AC2729}"
99+
EndProject
98100
Global
99101
GlobalSection(SolutionConfigurationPlatforms) = preSolution
100102
Debug|Any CPU = Debug|Any CPU
@@ -249,6 +251,10 @@ Global
249251
{74CE25AC-D9ED-44FB-8111-0D10D094D4CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
250252
{74CE25AC-D9ED-44FB-8111-0D10D094D4CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
251253
{74CE25AC-D9ED-44FB-8111-0D10D094D4CA}.Release|Any CPU.Build.0 = Release|Any CPU
254+
{A2C8D860-41F8-40B8-B939-BCA253AC2729}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
255+
{A2C8D860-41F8-40B8-B939-BCA253AC2729}.Debug|Any CPU.Build.0 = Debug|Any CPU
256+
{A2C8D860-41F8-40B8-B939-BCA253AC2729}.Release|Any CPU.ActiveCfg = Release|Any CPU
257+
{A2C8D860-41F8-40B8-B939-BCA253AC2729}.Release|Any CPU.Build.0 = Release|Any CPU
252258
EndGlobalSection
253259
GlobalSection(SolutionProperties) = preSolution
254260
HideSolutionNode = FALSE

Weaver/Core/Commands/DeleteValueCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
using Weaver.Interfaces;
1010
using Weaver.Messages;
1111

12-
namespace Weaver.Core
12+
namespace Weaver.Core.Commands
1313
{
1414
/// <inheritdoc />
1515
/// <summary>
1616
/// Mostly internal command, deletes a value from the Scripter registry.
1717
/// </summary>
18-
/// <seealso cref="Weaver.Interfaces.ICommand" />
18+
/// <seealso cref="ICommand" />
1919
public sealed class DeleteValueCommand : ICommand
2020
{
2121
/// <summary>

Weaver/ScriptEngine/Lowering.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
//TODO the node exit for DoWhile is currently not implemented in the ScriptExecutor
1111

12-
using System.Diagnostics;
1312
using System.Text.RegularExpressions;
1413
using Weaver.Interfaces;
1514
using Weaver.Messages;

0 commit comments

Comments
 (0)