Hello,
I have recently moved my project from one pc to another and from then the hell begun.I currently have a problem with my binding although in the recent version,everything was working properly.So the problem in my case at the moment is my combobox.I\m using MVVM and I have a method which I pass to a DelegateCommand and finally to the View itself.This is my method:
public void CreateCrazy(object para)
{
var retur = new List<AvailableCours>();
using (AnotherDbEntities db = new AnotherDbEntities())
{
try
{
var testing = Education;
var query = (from data in db.AvailableCourses where data.education == Education select new { CourseName = data.courseName }).ToList().Select(c => new AvailableCours { courseName = c.CourseName }).ToList();
retur = query;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Courses = new ObservableCollection<AvailableCours>(retur);
}
Basically the method shows all the courses available for that specific program.I have the programs listed in the combobox(so the binding with the combobox is correct) but I have a problem when I click the button and I want my courses displayed.Now,this is the binding in the view:
View.xaml.cs
RegisterTeacherViewModel regTeacher;
Object obj = new object();
public Register()
{
InitializeComponent();
regTeacher = new RegisterTeacherViewModel();
this.DataContext = regTeacher;
cbxCourses.ItemsSource = regTeacher.GetByEducation();
}
View.xaml
<ComboBox HorizontalAlignment="Left" x:Name="cbxCourses" SelectedItem="{Binding Education}" Margin="126,229.2,0,0"
IsSynchronizedWithCurrentItem="True" Grid.Row="2" VerticalAlignment="Top" DisplayMemberPath="education" Width="228" Grid.RowSpan="2"/>
<Button Content="Submit" Command="{Binding Path=SaveCommand,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" CommandParameter="{Binding ElementName=coursesList}" HorizontalAlignment="Left" Margin="517,98.4,0,0" Grid.Row="3" VerticalAlignment="Top" Width="110" Height="40"/>
<Button Content="Cancel" HorizontalAlignment="Left" Margin="361,98.4,0,0" Grid.Row="3" VerticalAlignment="Top" Width="111" Height="40"/>
<Button Content=">>" Name="ShowCourse" CommandParameter="{Binding ElementName=cbxCourses,Path=SelectedValue}" Command="{Binding MergeCommand,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="422,216.2,0,0" Grid.Row="2" VerticalAlignment="Top" Width="84" Height="33" Grid.RowSpan="2"/>
Here I have the binding with the combobox where I bind the education value,and the last button is the button with the courses.When I click the button,the courses should be displayed,instead there is nothing.I put a breakpoint to see the exact problem,and what I have noticed was that when I replace the data.education="ICT Engineering" ,I can see that the courses are added to the Courses list.When I have data.education=Education,nothing happens.What would be the problem?I double checked the values,the binding,everything but I can't seem to find the problem.Any advice would be appreciated!
This is "Education" value in case is relevant:
public String education = string.Empty;
public String Education
{
get { return education; }
set
{
education = value;
NotifyOnPropertyChange("Education");
}
}
private ObservableCollection<AvailableCours> _courses;
public ObservableCollection<AvailableCours> Courses
{
get => _courses;
set
{
_courses = value;
NotifyOnPropertyChange(nameof(Courses));
}
}
public RegisterTeacherViewModel()
{
mergeCommand = new DelegateCommand(CreateCrazy);
saveCommand = new DelegateCommand(SaveTeacher);
Courses = new ObservableCollection<AvailableCours>();
}
My problem is when I'm debugging and I put a breakpoint at the line of code where the education appears,it breaks without going into the view,although it should have break when I selected the value from the combobox.This is my method for the education:
public IEnumerable<AvailableCours> GetByEducation()
{
using (var context = new AttendanceStudentsEntities())
{
var query = (from data in context.AvailableCourses select new { Education = data.education }).ToList().Select(c => new AvailableCours { education = c.Education }).ToList();
return query.ToList();
}
}
This is the binding in the View.xaml:
<ComboBox HorizontalAlignment="Left" x:Name="cbxCourses" SelectedItem="{Binding Education}" Margin="126,229.2,0,0"
IsSynchronizedWithCurrentItem="True" Grid.Row="2" VerticalAlignment="Top" DisplayMemberPath="education" Width="228" Grid.RowSpan="2"/>
Along with the datacontext in View.xaml.cs :
public partial class Register : Window
{
RegisterTeacherViewModel regTeacher;
Object obj = new object();
public Register()
{
InitializeComponent();
regTeacher = new RegisterTeacherViewModel();
this.DataContext = regTeacher;
cbxCourses.ItemsSource = regTeacher.GetByEducation();//here I get all the programms from the database.
//coursesList.ItemsSource = regTeacher.GetByEducation();
//regTeacher.CreateCrazy(ShowCourse);
}
}
What I have tried:
I have tried with ElementName in order to bind the listbox(where the courses are displayed)to the button,updatesourcetrigger for binding and i assigned the itemssource to the combobox in the view.xaml.cs.