MarcAnt01
MarcAnt01

Reputation: 75

Having issues binding combobox item source from an observable collection

I am working on a uwp app and the issue is that combobox is empty with no elements, I am also open for better ideas to do that (basically need the items of the combobox being between 1 and 24). I have also used the proper using for the observable collection in the c# page.

XAML <ComboBox x:Name="HoursCB" HorizontalAlignment="Center" Margin="5" ItemsSource="{x:Bind HoursCollection}"/>

C#

public ObservableCollection<int> HoursCollection = new ObservableCollection<int>();

        public void AddHours()
        {
            for (int i = 1; i <= 24; i++)
            {
                HoursCollection.Add(i);
            }            
        }```

Upvotes: 1

Views: 54

Answers (1)

mm8
mm8

Reputation: 169320

You don't even need a method. Just initialize the collection directly:

public ObservableCollection<int> HoursCollection = 
    new ObservableCollection<int>(Enumerable.Range(1, 24));

Upvotes: 2

Related Questions