Hi
I am trying to load an Enum into the XamWebComboEditor but the items are not showing up.
I have an the following Enum Declared
Public Enum eDaysOfTheWeek
[Unknown]
[Monday]
[Tuesday]
[Wednesday]
[Thursday]
[Friday]
[Saturday]
[Sunday]
End Enum
In my XAML file the following Resource is declared:
<elliptx:eDaysOfTheWeek x:Key="DaysOfWeek" d:IsDataSource="True"/>
My comboeditor is set up as follows:
<igCombo:XamWebComboEditor x:Name="xamwebcomboeditor_meetingday"
ItemsSource="{Binding Source={StaticResource DaysOfWeek}}"
IsEditable="False"
Height="26"/>
What am i missing here??
Regards
Andrew
Hi Andrew,
There is one posible approach:
You need to implement a ValueConverter in this way:
//Helper class, that returns Enum values as array
internal static class EnumValueCache { private static readonly IDictionary<Type, object[]> Cache = new Dictionary<Type, object[]>(); public static object[] GetValues(Type type) { if (!type.IsEnum) throw new ArgumentException("Type '" + type.Name + "' is not an enum"); object[] values; if (!Cache.TryGetValue(type, out values)) { values = type.GetFields() .Where(f => f.IsLiteral) .Select(f => f.GetValue(null)) .ToArray(); Cache[type] = values; } return values; }
internal static class EnumValueCache
{
private static readonly IDictionary<Type, object[]> Cache = new Dictionary<Type, object[]>();
public static object[] GetValues(Type type)
if (!type.IsEnum)
throw new ArgumentException("Type '" + type.Name + "' is not an enum");
object[] values;
if (!Cache.TryGetValue(type, out values))
values = type.GetFields()
.Where(f => f.IsLiteral)
.Select(f => f.GetValue(null))
.ToArray();
Cache[type] = values;
}
return values;
//ValueComverter implementation public class EnumValuesConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return null; else return EnumValueCache.GetValues(value.GetType()); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
public class EnumValuesConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
if (value == null)
return null;
else
return EnumValueCache.GetValues(value.GetType());
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
throw new NotImplementedException();
//XAML code:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<local:EnumValuesConverter x:Key="enumConverter"/>
<local:DaysOfTheWeek x:Key="DaysOfWeek" d:IsDataSource="True"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ig:XamComboEditor Grid.Column="0" ItemsSource="{Binding Source={StaticResource DaysOfWeek}, Mode=OneTime, Converter={StaticResource enumConverter}}"/>
</Grid>
Hope this can help :-) It is very close to your case :-)
Cheers!
Mihail
I am sure I am missing something here. I see you are creating a list of days and then binding the combo to that List. What is the point of having an ENUM if you are simply going to create a custom list. I was under the impression that when you bind to an ENUM the values in that enum are used. In this way you can simply add/subtract items from the ENUM.
I thought Control.ItemsSource=ENUM should simply provide the ENUM as the datasource?
What am I missing here??
Thanks
With that configuration, I guess my question is what is your datasource object coming out as? With an enum described as public enum DaysOfTheWeek{ Monday, Tuesday, Wednesday } and a combo editor set up as
With that configuration, I guess my question is what is your datasource object coming out as?
With an enum described as
Monday,
Tuesday,
Wednesday
and a combo editor set up as
<
ig:XamComboEditor x:Name="edit" IsEditable="false" Width="200" Height="30"></ig:XamComboEditor>
I was able to see the enum values setting up the data
List<DaysOfTheWeek> days = new List<DaysOfTheWeek>(); days.Add( DaysOfTheWeek.Monday); days.Add( DaysOfTheWeek.Tuesday); days.Add( DaysOfTheWeek.Wednesday); this.edit.ItemsSource = days;
List<DaysOfTheWeek> days = new List<DaysOfTheWeek>(); days.Add(
days.Add(
DaysOfTheWeek.Monday); days.Add(
DaysOfTheWeek.Tuesday); days.Add(
DaysOfTheWeek.Wednesday);
this.edit.ItemsSource = days;
So what is you data coming back as?