Today is Sunday
May 20, 2012

February 23, 2010

Bind with Enum in Silverlight

by Ashish Sehajpal — Categories: Development — Tags: , 11 Comments

Silverlight doesn’t have any kind of ObjEctDataProvider with it. So the famous example of binding the controls with an ObjectDataProvider doesn’t work with silverlight as it works with WPF :(

<UserControl.Resources>
<ObjectDataProvider MethodName=“GetValues” ObjectType=”{x:Type sys:Enum}“  x:Key=“AlignmentValues”>   <ObjectDataProvider.MethodParameters>
   
      <x:Type TypeName=“HorizontalAlignment” />
   </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

This can be done via code behind in the Silverlight.  Just add a reference to the Reflection assembly.
using System.Reflection;

Use this simple function to get IEnumerable collection out of Enum type.

        public IEnumerable<Enum> GetEnumValues(Enum enumeration)
        {
            return from gField in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)
                   select (Enum)gField.GetValue(enumeration);
        }

Just bind the results of this function with your control and its done !

 ComboFrom.ItemsSource = GetEnumValues(new SehajService.Currency());

11 Comments »

  1. WP Themes says:

    Nice dispatch and this enter helped me alot in my college assignement. Thanks you seeking your information.

  2. Justin Halls says:

    Just what i’ve been looking for – BUT, I dont understand what you apss to the GetEnumValues() function. In the example you use new SehajService.Currency(), but I have a simple enum. How do i convert this into an acceptable Enum type for the parameter?

  3. Justin Halls says:

    OK, it can be simplified a bit:-
    public IEnumerable GetEnumValues(Type enumType)
    {
    return from gField in enumType.GetFields(BindingFlags.Static | BindingFlags.Public)
    select (Enum)gField.GetValue(enumType);
    }

    and then the function is called simply by:-

    cbEventTypes.ItemsSource = GetEnumValues(typeof(CONDITIONAL_EVENT_TYPE));

    although it might benefit from some checks in the GetEnumValues function to ensure that the type passed actuall is an enum.

  4. WP Themes says:

    Good brief and this mail helped me alot in my college assignement. Thank you for your information.

  5. lancome marianne says:

    Your blog is very beautiful, can you tell me how to make.

  6. Pokerspiel says:

    hey your blog design is very nice, neat and fresh and with updated content, make people feel peace and I always enjoy browsing your site.

    - Norman

  7. poker spielen says:

    I should digg your post therefore more folks can look at it, very helpful, I had a hard time finding the results searching on the web, thanks.

    - Murk

  8. Online Casino Download says:

    good points and the details are more precise than somewhere else, thanks.

    - Thomas

  9. independent insurance association says:

    Great blog post, I have been after that…

    Darrell

  10. beston clint says:

    One again, your idea is very

    good.thank you!very much.

  11. segs vignos says:

    Lovely! Very well-done! :)

© 2012 Sehajpal.com All rights reserved - Have fun!