How can I manage the error:system.windows.data error: 40 : bindingexpression?

Posted by: eliza_diaconu2008 on 11 April 2018, 8:24 am EST

  • Posted 11 April 2018, 8:24 am EST

    Hello,
    I have a problem from some time with my project and only today I saw in my output window that this error existed(this is after I looked all over in my code and tried different approaches that didn't work).The error is as follows:
    System.Windows.Data Error: 40 : BindingExpression path error: 'IsSelected' property not found on 'object' ''Cours' (HashCode=56607127)'. BindingExpression:Path=IsSelected; DataItem='Cours' (HashCode=56607127); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1')
    I have a listbox and inside it,course names with checkbox.My goal is that when I click in the checkbox and the item is checked,it has to be saved in the db.I learned how to read the error today,but unfortunately it ruins my day because I don't know the exact problem of it.As a result,I will share with you fellow programmers in hope someone will know how to help me solve it.This is the View-Model(changed version):
    class RegisterTeacherViewModel : ViewModelBase, INotifyPropertyChanged
    {

    private DelegateCommand mergeCommand;
    public DelegateCommand MergeCommand
    {
    get { return mergeCommand; }
    set
    {
    if (mergeCommand != value)
    {
    mergeCommand = value;
    NotifyOnPropertyChange("MergeCommand");
    }
    }
    }
    private String _UserName;
    public string UserName
    {
    get { return _UserName; }
    set
    {
    if (_UserName != value)
    {
    _UserName = value;
    NotifyOnPropertyChange("UserName");
    }
    }
    }
    private int pwd;
    public int Pwd
    {
    get { return pwd; }
    set
    {
    if (pwd != value)
    {
    pwd = value;
    NotifyOnPropertyChange("Pwd");
    }
    }
    }

    private int SNTeacher;
    public int SerialNT
    {
    get { return SNTeacher; }
    set
    {
    if (SNTeacher != value)
    {
    SNTeacher = value;
    NotifyOnPropertyChange("SerialNT");
    }
    }
    }
    private string fullName;
    public string FullName
    {
    get { return fullName; }
    set
    {
    if (fullName != value)
    {
    fullName = value;
    NotifyOnPropertyChange("FullName");
    }
    }
    }
    private string courseName;
    public string CourseName
    {
    get { return courseName; }
    set
    {
    if (courseName != value)
    {
    courseName = value;
    NotifyOnPropertyChange("CourseName");
    }
    }
    }
    public String education = string.Empty;
    public String Education
    {
    get { return education; }
    set
    {
    education = value;
    NotifyOnPropertyChange("Education");
    }
    }

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

    public RegisterTeacherViewModel()
    {
    mergeCommand = new DelegateCommand(CreateCrazy);
    saveCommand = new DelegateCommand(SaveTeacher);
    // checkCommand = new DelegateCommand(SaveTeacher, null);


    }
    private bool selectedCourse;
    public bool SelectedCourse
    {
    get { return selectedCourse; }
    set
    {
    selectedCourse =!value;
    foreach(var course in Courses)
    {
    if(course.IsChecked==true)
    {
    course.IsChecked = selectedCourse;
    Courses.Add(course);

    }
    }
    }
    }

    private DelegateCommand saveCommand;
    public DelegateCommand SaveCommand
    {
    get { return saveCommand; }
    set
    {
    if (saveCommand != value)
    {
    saveCommand = value;
    NotifyOnPropertyChange("SaveCommand");
    }
    }
    }
    public IEnumerable<Cours> GetByEducation()
    {

    using (var context = new DatabaseStudentsEntitiesLastStand())
    {
    var query = (from data in context.Courses select new { Education = data.education }).ToList().Select(c => new Cours { education = c.Education }).ToList();

    return query.ToList();

    }

    }

    //private bool isChecked;
    //public bool Checked
    //{
    // get { return isChecked; }
    // set
    // {
    // isChecked = value;
    // NotifyOnPropertyChange("Checked");
    // }
    //}

    public void CreateCrazy(object para)
    {
    var retur = new List<Cours>();
    using (DatabaseStudentsEntitiesLastStand db = new DatabaseStudentsEntitiesLastStand())
    {
    try
    {
    var testing = Education;
    var query = (from data in db.Courses where data.education == testing select new { CourseName = data.courseName }).ToList().Select(c => new Cours { courseName = c.CourseName }).ToList();
    retur = query;
    } catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    Courses = new ObservableCollection<Cours>(retur);
    }
    //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");
    // }
    //}



    public void SaveTeacher(object param)
    {

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

    if (c.IsChecked == true && 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);

    }
    }
    }

    //using (DatabaseStudentsEntitiesLastStand db = new DatabaseStudentsEntitiesLastStand())
    //{
    // var query = (from t in db.RegisterTeachers
    // select new RegisterTeacher()
    // {
    // SNTeacher=t.SNTeacher,
    // UserName=t.UserName,
    // pwd=t.pwd,
    // fullName=t.fullName
    // }).ToList();

    //}

    This is the binding with the view:
    <Window x:Class="LastAndFinalVersion.View.Register"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:LastAndFinalVersion.ViewModel"
    xmlns:mm="clr-namespace:LastAndFinalVersion.Helpers"
    xmlns:ms="clr-namespace:LastAndFinalVersion.Model"
    mc:Ignorable="d"
    Title="Register" Height="700" Width="700" FontSize="14" Background="#04c582" HorizontalAlignment="Center">
    <Window.DataContext>
    <local:RegisterTeacherViewModel/>

    </Window.DataContext>
    <Grid Margin="20,20,3.6,28.4">

    <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="2*"/>
    <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>
    <Label Content="Register"


    Foreground="White" FontSize="25" HorizontalAlignment="Center" Grid.ColumnSpan="2" Margin="179,0,175.6,0.4"/>
    <Separator>
    </Separator>

    <Label Content="SN:" HorizontalAlignment="Left" Margin="10,13.2,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Username: " HorizontalAlignment="Left" Margin="10,63.6,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="-0.833,-0.208"/>
    <Label Content="Password:" HorizontalAlignment="Left" Margin="10,111.6,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label Content="Full name: " HorizontalAlignment="Left" Margin="10,158.2,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="0.651,-0.415"/>
    <Label Content="Courses teaching: " HorizontalAlignment="Left" Margin="0,229.2,0,0" Grid.Row="1" VerticalAlignment="Top" Grid.RowSpan="2"/>
    <TextBox HorizontalAlignment="Left" Name="SerialNr" Height="23" Margin="126,13.6,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding SerialNT}" VerticalAlignment="Top" Width="248" Grid.ColumnSpan="2"/>
    <TextBox Grid.ColumnSpan="2" Name="UsernameTeacher" HorizontalAlignment="Left" Height="23" Margin="126,63.6,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding UserName}" VerticalAlignment="Top" Width="248"/>
    <PasswordBox Name="passwordBox" mm:PasswordHelper.BindPassword="true"
    mm:PasswordHelper.BoundPassword="{Binding Path=Pwd, Mode=TwoWay, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"
    PasswordChar="*" HorizontalAlignment="Left" Height="27" Margin="126,111.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="248"/>
    <TextBox HorizontalAlignment="Left" Name="fullName" Height="23" TextWrapping="Wrap" Text="{Binding FullName}" VerticalAlignment="Top" Width="248" Grid.ColumnSpan="2" Margin="126,158.6,0,0" Grid.Row="1" Grid.RowSpan="2"/>

    <ComboBox HorizontalAlignment="Left" DataContextChanged="cbxCourses_DataContextChanged" Text="{Binding Education}" x:Name="cbxCourses" SelectedItem="{Binding Education}" Margin="126,229.2,0,0"
    IsSynchronizedWithCurrentItem="True" Grid.Row="1" VerticalAlignment="Top" DisplayMemberPath="education" Width="228" Grid.RowSpan="2">

    <!--<StackPanel Orientation="Horizontal">
    <Image Source="https://www.frostburg.edu/fsu/assets/Image/dept/educ/education_sign_resized.jpg" Height="20"></Image>
    <TextBlock Text="Program"></TextBlock>
    </StackPanel>-->


    </ComboBox>
    <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" SelectionMode="Multiple" Name="coursesList" Height="240" SelectedItem="{Binding Path=SelectedCourse}" Margin="418,13.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="225" Grid.RowSpan="2" ItemsSource="{Binding Courses}" >
    <ListBox.ItemTemplate>
    <DataTemplate>
    <!--<ListBoxItem IsSelected="{Binding Path=IsChecked,ElementName=CheckBoxCourses}">-->
    <CheckBox x:Name="CheckBoxCourses" Content="{Binding Path=courseName}"
    Margin="0"
    IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" >
    <!--CommandParameter="{Binding Path=SelectedItem,RelativeSource={RelativeSource AncestorType=ListBox}}"-->

    <!--Command="{Binding Path=CheckCommand,Mode=OneWay}"-->

    </CheckBox>
    <!--</ListBoxItem>-->

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

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


    </Window>

    And this is my model(there i have IsChecked property to which I have a reference in the VM. Initially I made it in the View-Model applying the NotifyPropertyChanged,but since I saw the error today,I moved it here):
    public partial class Cours:INotifyPropertyChanged
    {
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Cours()
    {
    this.RegisterTeachers = new ObservableCollection<RegisterTeacher>();
    }

    public string courseName { get; set; }
    public int SNTeacher { get; set; }
    public string education { get; set; }
    public string ClassID { get; set; }
    public int courseID { get; set; }
    private bool isChecked;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool IsChecked
    {
    get { return isChecked; }
    set
    {
    if(isChecked!=value)
    {
    isChecked = value;
    NotifyOnPropertyChange("IsChecked");
    }
    }
    }
    protected void NotifyOnPropertyChange(String propertyName)
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    }

    public virtual RegisterTeacher RegisterTeacher { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ObservableCollection<RegisterTeacher> RegisterTeachers { get; set; }
    }
    }

    I provided full info about my code since I don't know exactly the cause and for better understanding of what I wish to accomplish.
  • Replied 13 April 2018, 9:36 am EST

    Hello,

    Sorry, it does not look like an issue related to Spread for WPF but a generic issue related to binding a boolean property to CheckBox's checked property. Looking at your code it seems you "IsSelected" should be replaced with "isChecked" in
    "IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" "

    Please refer to the code below to know how to bind the IsChecked property of CheckBox:


    <UserControl x:Class="Mockup.TestTab" ......>
    <!-- a checkbox somewhere within the control -->
    <!-- IsChecked is bound to Property C1 of the DataContext -->
    <CheckBox Content="CheckBox 1" IsChecked="{Binding C1, Mode=TwoWay}" />
    </UserControl>
    //In code behind for UserControl

    public partial class TestTab : UserControl
    {
    public TestTab()
    {
    InitializeComponent(); // the standard bit

    // then we set the DataContex of TestTab Control to a MyViewModel object
    // this MyViewModel object becomes the DataContext for all controls
    // within TestTab ... including our CheckBox
    DataContext = new MyViewModel(....);
    }

    }
    //Somewhere in solution class MyViewModel is defined

    public class MyViewModel : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;
    private bool m_c1 = true;

    public bool C1 {
    get { return m_c1; }
    set {
    if (m_c1 != value) {
    m_c1 = value;
    if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs("C1"));
    }
    }
    }
    }




    Thanks,
    Deepak Sharma
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Replied 18 April 2018, 3:05 am EST

    Hello,thank you for your response.This was one of the many solutions that I have tried.I have also tried the way you said and it doesn't work at all.I have this problem for 2 weeks and I don't know how to solve it.I have tried everything,I have made a separate class only for the isChecked value and selecteditem,still nothing.
  • Marked as Answer

    Replied 19 April 2018, 10:30 am EST

    Hello,

    Please have a look at the attached sample application which shows a simple demonstration of binding IsChecked property of CheckBox .



    Thanks,
    Deepak SharmaSpreadWpfCheckBoxChecked.zip
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels