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. You can leave a response or trackback from your own site. |
about 4 months ago
Nice dispatch and this enter helped me alot in my college assignement. Thanks you seeking your information.
about 3 months 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 3 months 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 2 months ago
Good brief and this mail helped me alot in my college assignement. Thank you for your information.
about 2 months ago
Your blog is very beautiful, can you tell me how to make.