Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have recently moved my project from one pc to another and from then the hell begun.I currently have a problem with my binding although in the recent version,everything was working properly.So the problem in my case at the moment is my combobox.I\m using MVVM and I have a method which I pass to a DelegateCommand and finally to the View itself.This is my method:

public void CreateCrazy(object para)
       {
           var retur = new List<AvailableCours>();
           using (AnotherDbEntities db = new AnotherDbEntities())
           {
               try
               {
                   var testing = Education;
                   var query = (from data in db.AvailableCourses where data.education == Education select new { CourseName = data.courseName }).ToList().Select(c => new AvailableCours { courseName = c.CourseName }).ToList();
                   retur = query;
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }
           Courses = new ObservableCollection<AvailableCours>(retur);
       }

Basically the method shows all the courses available for that specific program.I have the programs listed in the combobox(so the binding with the combobox is correct) but I have a problem when I click the button and I want my courses displayed.Now,this is the binding in the view:

View.xaml.cs

RegisterTeacherViewModel regTeacher;
       Object obj = new object();


       public Register()
       {
           InitializeComponent();
           regTeacher = new RegisterTeacherViewModel();
           this.DataContext = regTeacher;

           cbxCourses.ItemsSource = regTeacher.GetByEducation();
           //coursesList.ItemsSource = regTeacher.GetByEducation();
           //regTeacher.CreateCrazy(ShowCourse);

       }

View.xaml

<ComboBox HorizontalAlignment="Left"  x:Name="cbxCourses"  SelectedItem="{Binding Education}" Margin="126,229.2,0,0"
                IsSynchronizedWithCurrentItem="True" Grid.Row="2" VerticalAlignment="Top" DisplayMemberPath="education"  Width="228" Grid.RowSpan="2"/>
       <Button Content="Submit" Command="{Binding Path=SaveCommand,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" CommandParameter="{Binding ElementName=coursesList}"  HorizontalAlignment="Left" Margin="517,98.4,0,0" Grid.Row="3" VerticalAlignment="Top" Width="110" Height="40"/>
       <Button Content="Cancel"  HorizontalAlignment="Left" Margin="361,98.4,0,0" Grid.Row="3" VerticalAlignment="Top" Width="111" Height="40"/>

       <Button Content=">>" Name="ShowCourse"  CommandParameter="{Binding ElementName=cbxCourses,Path=SelectedValue}" Command="{Binding MergeCommand,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="422,216.2,0,0" Grid.Row="2" VerticalAlignment="Top" Width="84" Height="33" Grid.RowSpan="2"/>

Here I have the binding with the combobox where I bind the education value,and the last button is the button with the courses.When I click the button,the courses should be displayed,instead there is nothing.I put a breakpoint to see the exact problem,and what I have noticed was that when I replace the data.education="ICT Engineering" ,I can see that the courses are added to the Courses list.When I have data.education=Education,nothing happens.What would be the problem?I double checked the values,the binding,everything but I can't seem to find the problem.Any advice would be appreciated!

This is "Education" value in case is relevant:

 public String education = string.Empty;
        public String Education
        {
            get { return education; }
            set
            {
                education = value;
                NotifyOnPropertyChange("Education");
            }
        }

        private ObservableCollection<AvailableCours> _courses;
        public ObservableCollection<AvailableCours> Courses
        {
            get => _courses;
            set
            {
                _courses = value;
                NotifyOnPropertyChange(nameof(Courses));
            }
        }

        public RegisterTeacherViewModel()
        {
            mergeCommand = new DelegateCommand(CreateCrazy);
            saveCommand = new DelegateCommand(SaveTeacher);
            Courses = new ObservableCollection<AvailableCours>();
}



My problem is when I'm debugging and I put a breakpoint at the line of code where the education appears,it breaks without going into the view,although it should have break when I selected the value from the combobox.This is my method for the education:
public IEnumerable<AvailableCours> GetByEducation()
        {

            using (var context = new AttendanceStudentsEntities())
            {
                var query = (from data in context.AvailableCourses select new { Education = data.education }).ToList().Select(c => new AvailableCours { education = c.Education }).ToList();

                return query.ToList();

            }

        }

This is the binding in the View.xaml:
<ComboBox HorizontalAlignment="Left"  x:Name="cbxCourses"  SelectedItem="{Binding Education}" Margin="126,229.2,0,0"
                IsSynchronizedWithCurrentItem="True" Grid.Row="2" VerticalAlignment="Top" DisplayMemberPath="education"  Width="228" Grid.RowSpan="2"/>




Along with the datacontext in View.xaml.cs :
public partial class Register : Window
{
RegisterTeacherViewModel regTeacher;
Object obj = new object();


public Register()
{
InitializeComponent();
regTeacher = new RegisterTeacherViewModel();
this.DataContext = regTeacher;

cbxCourses.ItemsSource = regTeacher.GetByEducation();//here I get all the programms from the database.
//coursesList.ItemsSource = regTeacher.GetByEducation();
//regTeacher.CreateCrazy(ShowCourse);

}

}

What I have tried:

I have tried with ElementName in order to bind the listbox(where the courses are displayed)to the button,updatesourcetrigger for binding and i assigned the itemssource to the combobox in the view.xaml.cs.
Posted
Updated 2-May-18 1:56am
v2

You should compile and start your project in Debug Mode in VS and use your app as good as you can. Do you see a similar error as reported here How can I manage the error:system.windows.data error: 40 : bindingexpression path error: 'isselected' property not found on 'object' ''cours' ?[^] ?

System.Windows.Data Error: 40 : BindingExpression path error: 'CheckCommand' property not

If yes, you should now be able to see the missing property or why the binding is not working.

If no, you can also set breakpoints in the setter of the properties where you are missing the binding. Are the setters working as expected? Is the value set non-null or is it null (the later would also explain why you binding is not working).
 
Share this answer
 
Comments
Daniel Andrei Popescu 26-Apr-18 7:33am    
Thank you for your response.No,my error is different now.I set the breakpoint,but the value is null,but when I replace it with a string,it works by taking the courses for the program that I have just written as a string.The problem is that the binding is correct,I don't get any error and when I had the project on the previous PC,it worked perfectly.I have noticed anyhow that my database is constructed twice every time I make a new one.Maybe that is a problem?
Dirk Bahle 26-Apr-18 7:56am    
OK, so we eliminated 2 possible problems - a third frequently occuring problem is that your viewmodel does not implement the INotifyPropertyChanged interface - which in turn, sometimes leads to a situation you are describing:

- a value is displayed if the timing is lucky (on your old cumputer)
- a value is NOT displayed if the timing is such that the view is constructed and the value is changed (without notification) in the viewmodel after the construction of the view.

The public void CreateCrazy(object para) method looks strange to me. Try using the Observable Collection without a new constructor everytime you execute the method. You should instead:
1- construct the ObservableCollection in your standard constructor (only ONCE)
2- use Clear(), Add(), Remove() methods to manipulate the data in the collection

Does that change your problem in any way?
C#
Courses = new ObservableCollection<AvailableCours>(retur);
If the Courses variable is already created, the above line of code will break the binding and the new items will not be reflected in the view.

So, to fix, you need to check if the variable is created first. If it is, then you need clear the collection before adding:
C#
if (Courses == null)
    Courses = new ObservableCollection<AvailableCours>();
else
    Courses.Clear();

foreach (var item in retur)
    Courses.Add(retur);
 
Share this answer
 
Comments
Daniel Andrei Popescu 2-May-18 7:33am    
Thank you sir for your response,but unfortunately is not working.I still don't get anything when I click the button.
Daniel Andrei Popescu 2-May-18 7:51am    
Sir,now I have noticed that when I want to debug and I'm setting the breakpoint for the method where I get the education in the combobox,it breaks directly into it without entering the application and breaking when I select the education.What could be the problem?I have looked at the binding and everything is ok.

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

  Print Answers RSS


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