Skip to content

Commit 7f31230

Browse files
author
Wayfarer
committed
Add a new Converter
Sync back some Control changes.
1 parent 06de997 commit 7f31230

7 files changed

Lines changed: 96 additions & 34 deletions

File tree

CommonControls.Converter/BooleanToVisibilityConverter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
4141
{
4242
if (value is bool b)
4343
return b ? Visibility.Visible : (Collapse ? Visibility.Collapsed : Visibility.Hidden);
44-
4544
return Visibility.Hidden;
4645
}
4746

@@ -56,6 +55,6 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
5655
/// A converted value. If the method returns <see langword="null" />, the valid null value is used.
5756
/// </returns>
5857
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
59-
=> value is Visibility and Visibility.Visible;
58+
=> value is Visibility.Visible;
6059
}
61-
}
60+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: CommonControls.Converter
4+
* FILE: ColorToNameConverter.cs
5+
* PURPOSE: Convert between Color and its name as string (e.g., "Red", "Blue", etc.) for WPF bindings.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
10+
using System;
11+
using System.Globalization;
12+
using System.Windows.Data;
13+
using System.Windows.Media;
14+
using System.Linq;
15+
16+
namespace CommonControls.Converters
17+
{
18+
/// <summary>
19+
/// Converter that converts between a Color and its name as a string for WPF bindings. It uses reflection to find the name of the color in System.Windows.Media.Colors when converting from Color to string, and uses ColorConverter to convert from string to Color.
20+
/// </summary>
21+
/// <seealso cref="System.Windows.Data.IValueConverter" />
22+
public class ColorToNameConverter : IValueConverter
23+
{
24+
/// <summary>
25+
/// Convert from Color (ViewModel) to string (Control)
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+
if (value is Color color)
37+
{
38+
// Find the name of the color in System.Windows.Media.Colors
39+
var colorProperty = typeof(Colors).GetProperties()
40+
.FirstOrDefault(p => (Color)p.GetValue(null, null) == color);
41+
42+
return colorProperty?.Name ?? color.ToString();
43+
}
44+
return string.Empty;
45+
}
46+
47+
/// <summary>
48+
/// Convert from string (Control) to Color (ViewModel)
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+
{
59+
if (value is string colorName && !string.IsNullOrEmpty(colorName))
60+
{
61+
try
62+
{
63+
return ColorConverter.ConvertFromString(colorName);
64+
}
65+
catch { return Colors.Transparent; }
66+
}
67+
return Colors.Transparent;
68+
}
69+
}
70+
}

CommonControls.Converter/EnumToBooleanConverter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
5050
{
5151
if ((bool)value)
5252
return Enum.Parse(targetType, parameter.ToString()!);
53-
5453
return Binding.DoNothing;
5554
}
5655
}
57-
}
56+
}

CommonControls.Converter/NotNullToBooleanConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
4444
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
4545
=> Binding.DoNothing;
4646
}
47-
}
47+
}

CommonControls.Converter/NullToVisibilityConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
5353
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
5454
=> Binding.DoNothing;
5555
}
56-
}
56+
}

CommonControls.Images/ColorPick.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ public enum Area
6464
/// </summary>
6565
private static int OuterRadius => ColorPickerRegister.Size / 2;
6666

67-
internal static void Initiate(double h, double s, double v)
68-
{
69-
_sat = s;
70-
_val = v;
71-
_hue = h;
72-
}
73-
7467
/// <summary>
7568
/// Hue might be changed. Redraw the Circle and change up Hue.
7669
/// </summary>

CommonControls.Images/ColorSelection.xaml.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public sealed partial class ColorSelection
5353
public ColorSelection()
5454
{
5555
InitializeComponent();
56+
Loaded += (s, e) => Initiate(); // Ensure UI elements exist
5657
}
5758

5859
/// <inheritdoc />
@@ -62,8 +63,8 @@ public ColorSelection()
6263
/// <param name="color">The color.</param>
6364
public ColorSelection(string color)
6465
{
65-
StartColor = color;
6666
InitializeComponent();
67+
StartColor = color;
6768
}
6869

6970
/// <summary>
@@ -75,7 +76,6 @@ public string StartColor
7576
set
7677
{
7778
SetValue(StartColorProperty, value);
78-
SwitchColor();
7979
}
8080
}
8181

@@ -105,24 +105,16 @@ private void ColorSelection_Loaded(object sender, RoutedEventArgs e)
105105
/// </summary>
106106
private void Initiate()
107107
{
108-
//Generate Color Dictionary
109-
_colorDct = InitiateColors();
108+
var properties = typeof(Colors).GetProperties();
109+
_colorDct = properties.ToDictionary(
110+
p => p.Name,
111+
p => (Color)p.GetValue(null, null)
112+
);
110113

111-
try
112-
{
113-
//Fill the ComboBox
114-
CmbColor.ItemsSource = typeof(Colors).GetProperties();
115-
//Generate Color Dictionary
116-
_colorDct = InitiateColors();
117-
//Generate a possible List of Colors we can use from Code
118-
ColorPalette = _colorDct.Keys.ToList();
114+
CmbColor.ItemsSource = properties;
115+
ColorPalette = _colorDct.Keys.ToList();
119116

120-
SwitchToStartColor();
121-
}
122-
catch (ArgumentException ex)
123-
{
124-
ShowErrorMessageBox(ComCtlResources.ErrorInitializingColorSelection, ex);
125-
}
117+
SwitchToStartColor();
126118
}
127119

128120
/// <summary>
@@ -187,9 +179,18 @@ private static Dictionary<string, Color> InitiateColors()
187179
/// </summary>
188180
private void SwitchToStartColor()
189181
{
190-
if (string.IsNullOrEmpty(StartColor)) return;
182+
// Safety check: if the dictionary isn't built or ComboBox isn't ready, bail.
183+
if (string.IsNullOrEmpty(StartColor) || _colorDct == null || CmbColor == null)
184+
{
185+
return;
186+
}
191187

192-
CmbColor.SelectedItem = typeof(Colors).GetProperty(StartColor);
188+
// Use the dictionary to find the property info to ensure consistency
189+
var property = typeof(Colors).GetProperty(StartColor);
190+
if (property != null)
191+
{
192+
CmbColor.SelectedItem = property;
193+
}
193194
}
194195

195196
/// <summary>

0 commit comments

Comments
 (0)