Click here to Skip to main content
15,914,010 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
        }

    }

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


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"/>
Posted
Updated 15-Mar-18 22:55pm

1 solution

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>
 
Share this answer
 
Comments
Richard Deeming 16-Mar-18 13:18pm    
Question: Posted 8hrs 10mins ago
Answer: Posted 8hrs 5mins ago
Conclusion: You already knew the answer when you posted the question.
Daniel Andrei Popescu 4-Apr-18 3:57am    
Nope,I didn't because I don't waste time staying and formulating an entire description of my problem unless I really need help...Best regards!

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