Subclassed Properties Not Binding

RogerSchlueter-7899 1,196 Reputation points
2023-01-30T00:34:53.67+00:00

In my ViewModel I have this subclass:

Public Class Printing
    Public AccountName As Boolean
    Public AccountNumber As Boolean
    Public Expiration As Boolean
    Public SecurityCode As Boolean
End Class

which I use as follows:

Private _PrintList As New Printing
Public Property PrintList As Printing

This is referred to in the View as follows:

<CheckBox IsChecked="{Binding Path=PrintList.AccountName}" />

This gives a run-time error:

BindingExpression path error: 'AccountName' property not found on 'object' ''Printing

I do not understand why this binding fails.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} vote

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-01-30T00:40:33.7133333+00:00

    Databinding require property. You forgot to use Property keyword, and you have defined fields. You need to use Property keyword when defining properties; For example:

    Public Class Printing
        Public Property AccountName As Boolean
        Public Property AccountNumber As Boolean
        Public Property Expiration As Boolean
        Public Property SecurityCode As Boolean
    End Class
    
    

    For more information:


1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    2023-01-30T02:58:50.5633333+00:00

    As Reza Aghaei said, Databinding require property. You could refer to the following example.

     <Window.DataContext>
            <local:ViewModel />
        </Window.DataContext>
        <StackPanel>
            <CheckBox IsChecked="{Binding Path=PrintList.AccountName}" />
            <TextBox Text="{Binding PrintList.AccountName ,UpdateSourceTrigger=PropertyChanged ,Mode=TwoWay }"/>
        </StackPanel>
    
    Imports System.Collections.ObjectModel
    Imports System.ComponentModel
    Imports System.Windows.Automation
    
    Class MainWindow
    
    End Class
    Public Class ViewModel
    
        Public Property PrintList() As Printing = New Printing()
    
        Public Sub New()
            PrintList.AccountName = True
        End Sub
    
        Public Class Printing
            Implements INotifyPropertyChanged
    
            Private _AccountName As Boolean = False
            Public Property AccountName As Boolean
                Get
                    Return Me._AccountName
                End Get
    
                Set(ByVal value As Boolean)
                    Me._AccountName = value
                    NotifyPropertyChanged("AccountName")
                End Set
            End Property
            Public Property AccountNumber As Boolean
            Public Property Expiration As Boolean
            Public Property SecurityCode As Boolean
    
    
            Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
            Private Sub NotifyPropertyChanged(ByVal propertyName As String)
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
            End Sub
        End Class
    
    
    End Class
    
    

    The result:

    22


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments