Hello!
I have an WPF project using MVVM and i'm trying to bind the values from database to combobox thorugh linq queries.Everything works fine,but when I want to select a value in the combobox,I get the error "Object reference not set to an instance of an object".I tried to to it using FirstOrDefault() but everytime I did that,i couldn't get the values anymore in the combobox.
What should I do in order for the combobox selection to work?
What I have tried:
This is my view-model method:
private ObservableCollection<Cours> education;
public ObservableCollection<Cours> Education
{
get { return education; }
set
{
if (education != value)
{
education = value;
NotifyOnPropertyChange("Education");
}
}
}
public IEnumerable<Cours> GetByEducation()
{
using (var context = new DatabaseStudentsEntities1())
{
var query = (from data in context.Courses select new { Education = data.education }).ToList().Select(c => new Cours { education = c.Education }).ToList();
return query.ToList();
}
}
and this is the binding in the view:
<ComboBox HorizontalAlignment="Left" DataContextChanged="cbxCourses_DataContextChanged" x:Name="cbxCourses" SelectedItem="{Binding Education}" Margin="126,229.2,0,0" Grid.Row="1" VerticalAlignment="Top" DisplayMemberPath="education" Width="228" Grid.RowSpan="2">
</ComboBox>