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

I have changed the title of my question because I don't understand this error in the binding with the view.I know how to read it,but i don't understand it's cause or how should I change the syntax for it in a proper solution.This is my method for storing values:

View-Model:
public void SaveTeacher(object param)
        {

            using (DatabaseStudentsEntitiesLastStand db = new DatabaseStudentsEntitiesLastStand())
            {
              
                RegisterTeacher t = new RegisterTeacher();

                if (isChecked == true || t.CourseName != null)
                {
                    t.CourseName = courseName;

                }
                t.SNTeacher = SNTeacher;
                t.UserName = _UserName;
                t.pwd = pwd;
                t.fullName = fullName;
                t.education = education;

                db.RegisterTeachers.Attach(t);
               
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                   
                            MessageBox.Show(ex.Message);
                       
                }
            }
        }

This is the isChecked property:
private bool isChecked;
        public bool IsChecked
        {
            get
            {
                return IsChecked;
            }
            set
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    NotifyOnPropertyChange("IsChecked");
                }
            }
        }
        private DelegateCommand checkCommand;
        public DelegateCommand CheckCommand
        {
            get
            {
                if (checkCommand == null)
                    checkCommand = new DelegateCommand(SaveTeacher, null);
                return checkCommand;
            }
            set
            {
                checkCommand = value;
                NotifyOnPropertyChange("CheckCommand");
            }
        }

This is the binding with the view:
<Button Content="Submit" Command="{Binding SaveCommand}"  HorizontalAlignment="Left" Margin="517,98.4,0,0" Grid.Row="2" VerticalAlignment="Top" Width="110" Height="40"/>
        <Button Content="Cancel"  HorizontalAlignment="Left" Margin="361,98.4,0,0" Grid.Row="2" VerticalAlignment="Top" Width="111" Height="40"/>
        <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" Click="CheckBoxCourses_Click"
                              IsChecked="{Binding Path=IsChecked,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                           Command="{Binding CheckCommand,Mode=TwoWay}"

                            Content="{Binding Path=courseName,Mode=TwoWay}" Margin="0"/>


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

This is the Checked event:
private void CheckBoxCourses_Checked(object sender, RoutedEventArgs e)
        {
            var checkbox = sender as CheckBox;
            if(checkbox!=null)
            {
                checkbox.IsChecked =true;
            }


          
        }


What I have tried:

This is the entire error:
System.Windows.Data Error: 40 : BindingExpression path error: 'IsSelected' property not found on 'object' ''Cours' (HashCode=44335814)'. BindingExpression:Path=IsSelected; DataItem='Cours' (HashCode=44335814); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1')

I have tried multiple solutions with delegatecommand,selecteditem in the listbox and IsSelected property,read-only properties,nothing is working.Since I have tried many approaches,I have moved the property IsChecked from the view-model to the model class named "Cours" along with NotifyPropertyChanged(so it basically looks like the one I have in my example,only that it is in the model,not vm).Can someone please tell me what would be the problem based on this error?Please notify me if you need certain lines of code.
Posted
Updated 11-Apr-18 1:37am
v5
Comments
Aydin Homay 11-Apr-18 4:18am    
Hi Please copy also the method of CheckBoxCourses_Click to see how you are handling this event.
Daniel Andrei Popescu 11-Apr-18 4:26am    
Hello,thank you for your response.I did that already.

1 solution

1) Look at the output window in VS while you run the app in the debugger of VS - is there a statement about a Binding Expression Error?

This may not be the exact letters and words but you should see something similar if there is a binding problem which I think you might be having here...

2) I am not seeing a SelectedItem binding - I think thats one thing you are nissing to manipulate a particular item in the list.

Hope this helps ...
 
Share this answer
 
Comments
Daniel Andrei Popescu 11-Apr-18 4:27am    
Thank you for your response,I will try this approach and I'll come back with an answer.Regarding errors in this case,I don't see any error,everything runs perfectly except for the fact that the courseName is null thanks to the checkbox.
Dirk Bahle 11-Apr-18 4:35am    
This thread may be related to your problem: https://stackoverflow.com/questions/21193242/wpf-checkedlistbox-how-to-get-selected-item
Daniel Andrei Popescu 11-Apr-18 4:53am    
I have tried right now to do it by using the listbox but it's still not working...Thank you for your time but i don't think i'll manage to pass the error to soon.I work on it for 2 weeks,reviewing everything ans staring back again with the binding from scratch.I have no idea why is not working...
Daniel Andrei Popescu 11-Apr-18 6:18am    
I have looked at the output window and I see 2 errors for binding which are the following:
System.Windows.Data Error: 40 : BindingExpression path error: 'CheckCommand' property not found on 'object' ''Cours' (HashCode=1598569)'. BindingExpression:Path=CheckCommand; DataItem='Cours' (HashCode=1598569); target element is 'CheckBox' (Name=''); target property is 'Command' (type 'ICommand')

System.Windows.Data Error: 40 : BindingExpression path error: 'IsSelected' property not found on 'object' ''Cours' (HashCode=1598569)'. BindingExpression:Path=IsSelected; DataItem='Cours' (HashCode=1598569); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1).
Dirk Bahle 11-Apr-18 8:10am    
OK it looks like you are on track towards understanding your problem with these BindingExpression errors:
System.Windows.Data Error: 40 : BindingExpression path error: 'CheckCommand' property not found on 'object' ''Cours'

means that .Net attempts to bind you command property from the checkbox as specified with:
Command="{Binding CheckCommand,Mode=TwoWay}"
. to an object of type 'Cours' but cannot do it because the command property is simply missing (or not public) at the 'Cours' class.

The second error refers to the IsSelected property binding which can also not be bound because you are missing the property in the viewmodel that is attached to the MainWindow's DataContext.

You cannot expect that the Command binding will invoke the event handler: CheckBoxCourses_Checked as these are, albeit similar, completely different ways of getting a processing request done.

You should research MVVM and WPF Binding tutorial based on simpler scenarios to understand better how this works. This Tutorial maybe helpful:
http://www.wpf-tutorial.com/data-binding/introduction/

You will see similar error messages when you implement one of their example and then (willingly) change a binding specification such that it will not find a property in the viewmodel (e.g.: specify 'XXXX' instead of 'IsSelected'). Otherwise, there are plenty of MVVM WPF examples here on CodeProject.

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