EPLAN_PROD_Plugin/Sinvo.EplanHpD.Plugin.WPFUI/Converter/FlagEnumConverter.cs

33 lines
970 B
C#

using Sinvo.EplanHpD.Plugin.WPFUI.Enum;
using Sinvo.EplanHpD.Plugin.WPFUI.Utils;
using System;
using System.Globalization;
using System.Windows.Data;
namespace Sinvo.EplanHpD.Plugin.WPFUI.Converter
{
public class FlagEnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var flag = (WireFlagType)value;
if (!System.Enum.IsDefined(typeof(WireFlagType), flag))
{
return false;
}
return flag == (WireFlagType)parameter;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not bool isChecked)
{
throw new InvalidOperationException("The value must be a bool");
}
return isChecked ? (WireFlagType)parameter : Binding.DoNothing;
}
}
}