NetDocsForLLM/Converters/EnumBooleanConverter.cs

42 lines
1.2 KiB
C#

using System;
using System.Globalization;
using System.Windows.Data;
namespace NetDocsForLLM.Converters
{
/// <summary>
/// Converts between an enum value and a boolean value for binding to radio buttons or checkboxes
/// </summary>
public class EnumBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return false;
string checkValue = parameter.ToString();
string currentValue = value.ToString();
return checkValue.Equals(currentValue, StringComparison.InvariantCultureIgnoreCase);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return null;
bool isChecked = (bool)value;
if (isChecked)
{
if (parameter is string parameterString)
{
return Enum.Parse(targetType, parameterString);
}
return parameter;
}
return Binding.DoNothing;
}
}
}