当我想检查它们时,为什么我看不到列表框项? [英] Why can't I see the listbox items when I want to check them?

查看:68
本文介绍了当我想检查它们时,为什么我看不到列表框项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我有一个关于组合框中值的更新列表框。当我想添加复选框时,我可以从列表框中选择几个项目。在我给代码添加了复选框之前,我的列表项目显示正确。我添加后,我收到了复选框,但我看不到我正在检查的项目。会出现什么问题?



我的尝试:



这是我的VM:

Hello

I have a listbox that is updated regarding the values in the combobox.When I want to add checkboxes so that I can select few items from the listbox.Before I added the checkbox to the code,my list items were displayed correctly.After I added it,I get the checkbox but i can't see the item that i'm checking.What would be the problem?

What I have tried:

This is my VM:

  public class RegisterTeacherViewModel : ViewModelBase, INotifyPropertyChanged
    {
private List<Cours> courseName;
        public List<Cours> CourseName
        {
            get { return courseName; }
            set
            {
                if (courseName != value)
                    courseName = value;
                NotifyOnPropertyChange("CourseName");
            }
        }

      
        private String education;
        public String Education
        {
            get { return education; }
            set
            {
                if (education != value)
                {
                    education = value;
                    NotifyOnPropertyChange("Education");
                }
            }
        }

        private bool Ischecked;
        public bool Checked
        {
            get
            {
                return Ischecked ;
            }
            set
            {
                Ischecked = value;
                NotifyOnPropertyChange("IsChecked");
            }
        }
        
        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();
               
            }
           
        }


  public List<Cours> CreateCrazy()
        {
            using (DatabaseStudentsEntities1 db = new DatabaseStudentsEntities1())
            {
                try
                {
                    var query = (from data in db.Courses where data.education == "ICT Engineering" select new { CourseName = data.courseName }).ToList().Select(c => new Cours { courseName = c.CourseName }).ToList();
                    return query.ToList();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            return null;
        }


  public void SaveTeacher(object param)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\VIAApp2Demo\VIAApp2Demo\DB\DatabaseStudents.mdf;Integrated Security=True;Connect Timeout=30");
        try
        {
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();
            String query = "INSERT INTO RegisterTeacher (SNTeacher,UserName,pwd,fullName,CourseName,education) VALUES(@SNTeacher,@UserName,@pwd,@fullName,@CourseName,@education)";
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.CommandType =CommandType.Text;
            cmd.Parameters.AddWithValue("@UserName", UserName);
            cmd.Parameters.AddWithValue("@SNTeacher", SNTeacher);
            cmd.Parameters.AddWithValue("@pwd", pwd);
            cmd.Parameters.AddWithValue("@fullName", fullName);
                foreach(var item in CourseName)
                {
                    if(item.Checked)
                    {
                  SqlParameter cours = cmd.Parameters.AddWithValue("@courseName", SqlDbType.NVarChar);
                        cmd.Parameters["@courseName"].Value = courseName;
                        if(cours.Value==null)
                        {
                            cours.Value = DBNull.Value;
                        }
                    }
                }
           
            cmd.Parameters.AddWithValue("@education", education);

            cmd.ExecuteNonQuery();
            MessageBox.Show("Registration succesful!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }

    }



这是View.xaml.cs:




This is the View.xaml.cs:

 public partial class Login :Window
    {
       private LoginViewModel lg;
        public Login()
        {
            InitializeComponent();
            lg = new LoginViewModel();
            this.DataContext = lg;
        }

      
       
       

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Register reg = new Register();
            reg.Show();
            this.Close();
        }
    }
}





这是View.xaml:





This is the View.xaml:

<ListBox HorizontalAlignment="Left" Name="coursesList"  SelectedItem="{Binding CourseName}" Height="240"   Margin="418,13.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="225" Grid.RowSpan="2"  ItemsSource="{Binding CourseName, Mode=TwoWay}" >
           <ListBox.ItemTemplate>
               <DataTemplate>
                   <CheckBox x:Name="CheckBoxCourses" IsChecked="{Binding Checked,Mode=TwoWay}" ClickMode="Press" Content="" Margin="0"/>


               </DataTemplate>
           </ListBox.ItemTemplate>
       </ListBox>
       <Button Content="Show courses:" Name="ShowCourse" Command="{Binding Path=showCourse}" CommandParameter="{Binding ElementName=cbxCourses,Path=SelectedValue}" Click="Button_Click" HorizontalAlignment="Left" Margin="163,61.4,0,0" Grid.Row="2" VerticalAlignment="Top" Width="137" Height="35"/>

推荐答案

我发现问题:在View.xaml中。这是正确的解决方案:

I found the problem:is in View.xaml.This is the correct solution:
<ListBox HorizontalAlignment="Left" Name="coursesList"  SelectedItem="{Binding CourseName}" Height="240"   Margin="418,13.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="225" Grid.RowSpan="2"  ItemsSource="{Binding CourseName, Mode=TwoWay}" >
          <ListBox.ItemTemplate>
              <DataTemplate>
                  <CheckBox x:Name="CheckBoxCourses" IsChecked="{Binding Checked,Mode=TwoWay}"  ClickMode="Press" Content="{Binding Path=courseName}" Margin="0"/>


              </DataTemplate>
          </ListBox.ItemTemplate>
      </ListBox>


这篇关于当我想检查它们时,为什么我看不到列表框项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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