c# – IValueConverter删除枚举的基础值(int)

我将带有基础值(int)的枚举作为参数传递给XAML到IValueConverter.我们的想法是使用枚举值来迭代列表以检查值是否存在&返回一个可见性枚举.该列表的类型为int.但是当将Enum传递给转换器时,我无法将Enum强制转换为int.

使用“Quick Watch”查看参数,我根本看不到基础值或任何值.

例如,例如

public class Operations
{
    public enum Reporting
    {
        ReportAccounts    = 101,
        ReportEngineering = 102,
        ReportSoftware    = 103,
        ReportPR          = 104,
        ReportCRM         = 105
     }
     public enum Editing
     {
        EditUser   = 201,
        EditAccess = 202,
        EditView   = 203
     }
 }

XAML例如

Visibility={Binding Converter={StaticResource VisibilityConverter}, 
        ConverterParameter={x:Static Operations:Reporting.ReportAccounts}}

IValueConverter,例如

public object Convert(object value, Type targetType, object parameter, 
                      System.Globalization.CultureInfo culture)
{
     bool visibility = OperationList.Exists(list => list.Id == (int)parameter);
     if (visibility == true)
     {
         return Visibility.Visible;
     }
     else
     {
         return Visibility.Collapsed;
     }
 }

我想知道在传递给IValueConverter时是否有任何方法可以保留或检索枚举的基础值.

最佳答案 您可能必须先将参数强制转换为枚举类型报告:

int val = System.Convert.ToInt32((Reporting)parameter);
bool visibility = OperationList.Exists(list => list.Id == val);
点赞