No path is too difficult. No destination is too far !
Bind with Enum in Silverlight
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());
| Print article | This entry was posted by Ashish Sehajpal on February 23, 2010 at 4:54 pm, and is filed under Development. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.
about 1 year ago
Nice dispatch and this enter helped me alot in my college assignement. Thanks you seeking your information.
about 1 year ago
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?
about 1 year ago
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.
about 1 year ago
Good brief and this mail helped me alot in my college assignement. Thank you for your information.
about 1 year ago
Your blog is very beautiful, can you tell me how to make.
about 1 year ago
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
about 1 year ago
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
about 1 year ago
good points and the details are more precise than somewhere else, thanks.
- Thomas
about 1 year ago
Great blog post, I have been after that…
Darrell
about 1 year ago
One again, your idea is very
good.thank you!very much.
about 1 year ago
Lovely! Very well-done!