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

31 lines
878 B
C#
Raw Normal View History

2024-10-24 18:01:26 +08:00
using System;
using System.Globalization;
using System.Windows.Data;
namespace Sinvo.EplanHpD.Plugin.WPFUI.Utils
{
public class FlagEnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var flag = (WireFlagType)value;
if (!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;
}
}
}