2

I have an enum:

public enum UnitOfMeasure
{
    Meters,
    Kilometers,
    Yards,
    Miles,
    Time
}

and I want to bind it to a ListBox (actually a Telerik RadListPicker, but it works the same):

<telerikInput:RadListPicker
    Header="Measure work in:"
    ItemsSource="{Binding WorkUnitOfMeasure}"
    HeaderStyle="{StaticResource HeaderStyle}"
    x:Name="workUnitsListPicker"
    Margin="18">
</telerikInput:RadListPicker>

My View Model:

/// <summary>
/// The <see cref="WorkUnitOfMeasure" /> property's name.
/// </summary>
public const string WorkUnitOfMeasurePropertyName = "WorkUnitOfMeasure";

private ObservableCollection<Enum<UnitOfMeasure>> _workUnitOfMeasure;
/// <summary>
/// Gets the WorkUnitOfMeasure property.
/// Changes to that property's value raise the PropertyChanged event. 
/// This property's value is broadcasted by the Messenger's default instance when it changes.
/// </summary>
public ObservableCollection<Enum<UnitOfMeasure>> WorkUnitOfMeasure
{
    get
    {
        return _workUnitOfMeasure;
    }
    set
    {
        if (_workUnitOfMeasure == value)
        {
            return;
        }
        var oldValue = _workUnitOfMeasure;
        _workUnitOfMeasure = value;
         RaisePropertyChanged(WorkUnitOfMeasurePropertyName);
    }
}

And in my constructor I have tried variations of something like this:

WorkUnitOfMeasure = new ObservableCollection<Enum<UnitOfMeasure>>();

I can't seem to get the listbox to bind to Enum. I know I'm missing something simple, but I can't figure it out.

JeffCren
  • 396
  • 1
  • 5
  • 15
  • The heck is `Enum`? Not sure if WP7 has the ObjectDataSource, but if so this is a dupe of http://stackoverflow.com/questions/6145888/how-to-bind-an-enum-to-a-combobox-control-in-wpf –  Dec 22 '11 at 21:26
  • @JeffCren: Are you binding single enum or list of UnitOfMeasure enums? – Shashi Jan 03 '12 at 07:15
  • I am trying to bind the list of enums to a dropdown list. – JeffCren Jan 06 '12 at 15:12
  • @Will: WP7 does not have the ObjectDataSource. I saw that question before I posted mine. – JeffCren Jan 06 '12 at 15:47

2 Answers2

1

I believe it should be an

ObservableCollection<UnitOfMeasure>

not

ObservableCollection<Enum<UnitOfMeasure>

If not what error are you seeing?

chief7
  • 14,263
  • 14
  • 47
  • 80
  • I don't get an error, but the listbox just doesn't bind - it's empty. – JeffCren Jan 06 '12 at 15:13
  • I just noticed that in the viewmodel constructor, when I call WorkUnitOfMeasure = new ObservableCollection(); the count on WorkUnitOfMeasure is 0, so it's not getting initialized correctly. It may not be the binding actually, but the initializing. – JeffCren Jan 06 '12 at 15:20
  • 1
    @JeffCren: You do know that you have to *add the different values of the enum to the collection*, right? Its not like the collection fills itself. –  Jan 06 '12 at 15:52
0

If you are binding the single enum items to listbox:

Following code may help you:

 List<string> newList = new List<string>();
        for (int i = 0; i <= (int)UnitOfMeasure.Time; i++)
            newList.Add(Enum.GetName(typeof(UnitOfMeasure), i));
        SampleList.ItemsSource = newList;
Shashi
  • 2,860
  • 2
  • 34
  • 45