How can I create a DataBinding to an Enumeration value of an object? I.e., I want to have a ListBox (or a ComboBox/DropDownList, as soon as there is one available in Silverlight) and fill it with the enumeration members and DataBind its selection index to
a property of my DataContext object which is of the enumeration type.
In Silverlight there is no stratight forward way of iterating through an Enum so they can't be used as a datasource. However there is a way to do it. The following can be used to create an array of Enums and then used in code to fill a control.
public Enum[] EnumToArray(Enum enumeration)
{
//get the enumeration type
Type et = enumeration.GetType();
//get the public static fields (members of the enum)
System.Reflection.FieldInfo[] fi = et.GetFields(BindingFlags.Static |
BindingFlags.Public);
//create a new enum array
Enum[] values =
new Enum[fi.Length];
//populate with the values
for (int iEnum = 0; iEnum < fi.Length; iEnum++)
public IEnumerable<Enum> GetEnumValues(Enum enumeration)
{
return from f in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)
select (Enum)f.GetValue(enumeration);
}
cheeeeesus
0 Points
1 Post
DataBinding to an Enumeration
Aug 20, 2008 09:31 AM | LINK
Hi folks
How can I create a DataBinding to an Enumeration value of an object? I.e., I want to have a ListBox (or a ComboBox/DropDownList, as soon as there is one available in Silverlight) and fill it with the enumeration members and DataBind its selection index to a property of my DataContext object which is of the enumeration type.
In WPF, this is done using
mchlSync
Star
14968 Points
2799 Posts
Re: DataBinding to an Enumeration
Aug 24, 2008 07:39 AM | LINK
I think you have to set the binding from code. Please let me know if you are not able to set it from code behind.
Regards,
Michael Sync
Silverlight MVP
Blog : http://michaelsync.net
saavedrah
Member
13 Points
15 Posts
Re: DataBinding to an Enumeration
Nov 29, 2008 04:14 PM | LINK
Hi there,
Somebody resolved this issue ??
Michael said that the binding had to be done in the code. However, he did not say if a converter should be created or how the bind should be done.
I'm trying to bind an Enum to a ComboBox and it seams that it is not an easy way since the ObjectDataProvider is not available.
Any suggestion is appreciated
Thanks.
A sample of the binding to Enum can be found at Brant's WebLog let see how it works for me.
stbuchok
Member
65 Points
21 Posts
Re: Re: DataBinding to an Enumeration
Nov 29, 2008 09:30 PM | LINK
In Silverlight there is no stratight forward way of iterating through an Enum so they can't be used as a datasource. However there is a way to do it. The following can be used to create an array of Enums and then used in code to fill a control.
public Enum[] EnumToArray(Enum enumeration){
//get the enumeration type Type et = enumeration.GetType(); //get the public static fields (members of the enum) System.Reflection.FieldInfo[] fi = et.GetFields(BindingFlags.Static | BindingFlags.Public); //create a new enum array Enum[] values = new Enum[fi.Length]; //populate with the values for (int iEnum = 0; iEnum < fi.Length; iEnum++){
values[iEnum] = (Enum)fi[iEnum].GetValue(enumeration);}
//return the array return values;}
Code to fill a combobox. ScannableField is an Enum in a class called Enums.
Enum[] enumValues = EnumToArray(new Enums.ScannableField());foreach (Enum enums in enumValues){
cboScannableField.Items.Add(enums.ToString());
}
Hope this helps.
saavedrah
Member
13 Points
15 Posts
Re: Re: Re: DataBinding to an Enumeration
Dec 12, 2008 06:20 PM | LINK
Thank you,
It was posible to Bind the enumeration with this change.
snortblt
Member
34 Points
31 Posts
Re: Re: DataBinding to an Enumeration
Oct 08, 2009 10:31 PM | LINK
Or a little shorter with LINQ:
siteman
Member
102 Points
30 Posts
Re: Re: DataBinding to an Enumeration
Oct 08, 2009 11:49 PM | LINK
www.silverlightds.com
LeTrape
Member
39 Points
16 Posts
Re: DataBinding to an Enumeration
Dec 23, 2011 10:25 PM | LINK
snorblt How do i can call this method