Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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; }
   }
Posted
Updated 21-Mar-18 4:52am
v2
Comments
Graeme_Grant 21-Mar-18 6:42am    
"every time I select a value with the checkbox and then I want to store it,I see that the Checked property in false"

You have pasted db handling code, not your ViewModel nor the Model class that holds the data referred to in you ListBox DataTemplate Bindings. Have you tried setting a breakpoint in the model property Set for the Checked property?

" form with a combobox and a listbox.In the listbox I have checkboxes for selecting the specific courses"

Your DB code same is looking a the Checked property of the ViewModel and not the Checked property of each model. Which Checked property are you testing for and what is the relationship of each Model Checked property and the main form Checked property?
Daniel Andrei Popescu 21-Mar-18 6:55am    
Hello,CourseName is the property in the checkbox that I'm testing and about the relationship,I don't really understand what you're referring to.I am still a begginner with MVVM so i would really appreciate if you would rephrase the question. :)
Graeme_Grant 21-Mar-18 20:33pm    
Okay, I have highlighted the question above for clarity...

Have you tried setting a breakpoint in the model property Set [method] for the Checked property? Is the breakpoint hit and the code execution paused?
Daniel Andrei Popescu 22-Mar-18 4:15am    
Hello
Daniel Andrei Popescu 22-Mar-18 4:19am    
I have tried so many things yesterday and I was too tired at a certain point,I apologize for not answering earlier.I put a breakpoint and saw that the value was false.Initially I had my bool in the view-model.I tried another approach by declaring it in the model class and call it thorugh the reference to the model (reference was t) and i made something like-if(t.isChecked==true).This time I get the bool value in the view when I want to bind it,but still get the value false.I started then wondering that my problem is the binding since everything was working fine except for this.Then I started making some research and tried 3 different approaches of binding a checkbox,including a delegate command and a read-only property.Nothing worked...

1 solution

Start by putting a breakpoint on the set method in your Checked property, and verify that the binding you've set up is actually working.
 
Share this answer
 
Comments
Graeme_Grant 21-Mar-18 18:13pm    
Good to hear you repeat what I originally suggested...
#realJSOP 22-Mar-18 5:43am    
I read the question and immediately posted my answer without reading the comments, since comments don't usually include solutions. In point of fact, I still haven't read the comments and don't intend to. Your 1-vote is uncalled for.
Graeme_Grant 22-Mar-18 5:44am    
What makes you think that I voted... I only commented.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900