4

I'm passing an Enum with underlying value (int) as a parameter from XAML to an IValueConverter. The idea is to use the Enum value to iterate against a list to check if the value exists & return a Visibility enum. The list is of type int. But when passing the Enum to the converter, I cannot cast the Enum back to an int. Peering into the parameter with 'Quick Watch', I cannot see the underlying value or any value at all.

Enum e.g.

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 e.g.

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

IValueConverter e.g.

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;
     }
 }

I would like to know if there is any way of retaining or retrieving the underlying value of an enum when it is passed to an IValueConverter.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
SAm
  • 2,154
  • 28
  • 28
  • You say you are peering into *value*, but you pass the enum value as the `ConverterParameter`. Have you tried peering into `parameter` instead? – O. R. Mapper Apr 07 '13 at 16:40
  • Please format your source code properly. That formatting had already been done - not only in your code samples, but also in your message text - by a fellow SO user, but you just undid those changes and thereby made your question considerably less readable. – O. R. Mapper Apr 07 '13 at 16:52
  • is there a way to recall this post? I haven't really formatted the question? this is my first post. – SAm Apr 07 '13 at 16:54
  • 1
    Yes, see under your quesiton, where it says "edited ..." - that's a link that brings you to the editing history of your question :-) You can see diffs for each editing operation done so far. – O. R. Mapper Apr 07 '13 at 16:54
  • Found an earlier question that is related: http://stackoverflow.com/questions/4942501/using-enum-in-converterparameter – SAm May 13 '13 at 05:31

1 Answers1

2

You might have to cast the parameter to the enum-type Reporting first:

int val = System.Convert.ToInt32((Reporting)parameter);
bool visibility = OperationList.Exists(list => list.Id == val);
Florian Gl
  • 5,984
  • 2
  • 17
  • 30