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.