Reputation: 5143
I am lost... I checked everything I coud think of..
But the Items in my ComboBox are not displayed.
What else can be wrong here ?
Here's my code:
private void Refresh(bool isAuto)
{
List<DatabaseEntry> entries = GetBrokenJobs(isAuto);
Collection.Clear();
foreach (string jobName in entries.Select(x => x.Description).ToList())
{
Collection.Add(jobName);
}
}
I am binding to a ComboxBox:
ItemsSource="{Binding Path=Collection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
private ObservableCollection<string> _collection = new ObservableCollection<string>();
public ObservableCollection<string> Collection
{
get {return _collection; }
set
{
_collection = value;
OnPropertyChanged();//<= Has [CallerMemberName] in constructor ...
}
}
And I am setting the DataContext like always ..
this.DataContext = MainViewModel.Instance;
Since all my Buttons and CheckBoxes work this might not be the origin ..
EDIT
Here is some more Xaml
<ComboBox x:Name="ComboBox"
Grid.Row="1"
Margin="10"
Grid.Column="1"
Width="230"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
IsEnabled="{Binding Path=IsJobSelectorEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=SelectedJobItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=Collection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>
Upvotes: 0
Views: 318
Reputation: 95
I know it kind of goes against what an observablecollection should do but I have had a problem in the past where Adding to an existing ObservableCollection is completely ignored.
If you try replacing the whole observablecollection so something along the lines of, it may force it to be updated:
List entries = GetBrokenJobs(isAuto);
Collection = new ObservableCollection(entries.Select(x => x.Description).ToList());
Upvotes: 0
Reputation: 4474
The solution is simple. Bind the combobox item source to an ObservableCollection.
This is not required ItemsSource="{Binding Path=Collection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Please use like this ItemsSource="{Binding ComboBoxList}" where ComboBoxList is an observable collection defined inside your data context.
Upvotes: 1