为什么我的复选框中的bool bool值为false? [英] Why my cheked bool value from checkbox is false ?

查看:76
本文介绍了为什么我的复选框中的bool bool值为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我的项目有问题。我有一个带有组合框和列表框的注册表。在列表框中我有选中的复选框特定课程。我的问题是调试,因为每次我选中一个带复选框的值,然后我想存储它,我看到Checked属性为false。这是我的视图:

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);
                       }
                   }
               }
           }





我尝试了什么:



我首先使用db与Entity Framework。可能是什么问题?我检查了我的绑定,这是正确的,也许我应该在VM中使用另一种方法?



这是我的完整VM:







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; }
   }

推荐答案

首先在集上放置一个断点 Checked 属性中的code>方法,并验证你设置的绑定是否真的有效。
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.


这篇关于为什么我的复选框中的bool bool值为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆