Hello,
I have a problem with my project.I have a register form with a combobox and a listbox.In the listbox I have checkboxes for selecting the specific courses.My problem is on debugging because every time I select a value with the checkbox and then I want to store it,I see that the Checked property in false.This is my View:
<ListBox HorizontalAlignment="Left" Name="coursesList" Height="240" Margin="418,13.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="225" Grid.RowSpan="2" ItemsSource="{Binding Courses}" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="CheckBoxCourses" IsChecked="{Binding Path=Checked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ClickMode="Press" Content="{Binding Path=courseName}" Margin="0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is my view-model:
using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
{
RegisterTeacher t = new RegisterTeacher();
if(Checked==true)
{
t.CourseName = courseName;
}
t.SNTeacher = SNTeacher;
t.UserName = _UserName;
t.pwd = pwd;
t.fullName = fullName;
Cours c = new Cours();
c.education = education;
db.RegisterTeachers.Attach(t);
db.Courses.Add(c);
try {
db.SaveChanges();
}catch(DbEntityValidationException ex)
{
foreach(var entityValidationErrors in ex.EntityValidationErrors)
{
foreach(var validationError in entityValidationErrors.ValidationErrors)
{
MessageBox.Show("Property:" + validationError.PropertyName + "Error:" + validationError.ErrorMessage);
}
}
}
}
What I have tried:
I'm using db first with Entity Framework.What could be the problem?I checked my binding and it is correct,maybe I should use another approach in VM?
This is my full VM:
private DelegateCommand mergeCommand;
public DelegateCommand MergeCommand
{
get { return mergeCommand; }
set
{
if (mergeCommand != value)
{
mergeCommand = value;
NotifyOnPropertyChange("MergeCommand");
}
}
}
private String _UserName;
public string UserName
{
get { return _UserName; }
set
{
if (_UserName != value)
{
_UserName = value;
NotifyOnPropertyChange("UserName");
}
}
}
private int pwd;
public int Pwd
{
get { return pwd; }
set
{
if (pwd != value)
{
pwd = value;
NotifyOnPropertyChange("Pwd");
}
}
}
private int SNTeacher;
public int SerialNT
{
get { return SNTeacher; }
set
{
if (SNTeacher != value)
{
SNTeacher = value;
NotifyOnPropertyChange("SerialNT");
}
}
}
private string fullName;
public string FullName
{
get { return fullName; }
set
{
if (fullName != value)
{
fullName = value;
NotifyOnPropertyChange("FullName");
}
}
}
private string courseName;
public string CourseName
{
get { return courseName; }
set
{
if (courseName != value)
{
courseName = value;
NotifyOnPropertyChange("CourseName");
}
}
}
public String education = string.Empty;
public String Education
{
get { return education; }
set
{
education = value;
NotifyOnPropertyChange("Education");
}
}
private ObservableCollection<Cours> _courses;
public ObservableCollection<Cours> Courses
{
get => _courses;
set
{
_courses = value;
NotifyOnPropertyChange(nameof(Courses));
}
}
public RegisterTeacherViewModel()
{
mergeCommand = new DelegateCommand(CreateCrazy);
saveCommand = new DelegateCommand(SaveTeacher);
}
private DelegateCommand saveCommand;
public DelegateCommand SaveCommand
{
get { return saveCommand; }
set
{
if (saveCommand != value)
{
saveCommand = value;
NotifyOnPropertyChange("SaveCommand");
}
}
}
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();
}
}
private bool isChecked;
public bool Checked
{
get { return isChecked; }
set
{
isChecked = value;
NotifyOnPropertyChange("Checked");
}
}
public void CreateCrazy(object para)
{
var retur = new List<Cours>();
using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
{
try
{
var testing = Education;
var query = (from data in db.Courses where data.education == testing select new { CourseName = data.courseName }).ToList().Select(c => new Cours { courseName = c.CourseName }).ToList();
retur = query;
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Courses = new ObservableCollection<Cours>(retur);
}
public void SaveTeacher(object param)
{
using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
{
RegisterTeacher t = new RegisterTeacher();
if(Checked==true)
{
t.CourseName = courseName;
}
t.SNTeacher = SNTeacher;
t.UserName = _UserName;
t.pwd = pwd;
t.fullName = fullName;
Cours c = new Cours();
c.education = education;
db.RegisterTeachers.Attach(t);
db.Courses.Add(c);
try {
db.SaveChanges();
}catch(DbEntityValidationException ex)
{
foreach(var entityValidationErrors in ex.EntityValidationErrors)
{
foreach(var validationError in entityValidationErrors.ValidationErrors)
{
MessageBox.Show("Property:" + validationError.PropertyName + "Error:" + validationError.ErrorMessage);
}
}
}
}
}
And this is my Model class:
public partial class RegisterTeacher
{
public RegisterTeacher()
{
this.Logins = new ObservableCollection<Login>();
}
public int SNTeacher { get; set; }
public string UserName { get; set; }
public int pwd { get; set; }
public string fullName { get; set; }
public string CourseName { get; set; }
public virtual ObservableCollection<Login> Logins { get; set; }
}