Cannot Get a ComboBox to Show the Items Properly

RogerSchlueter-7899 1,236 Reputation points
2023-02-02T05:04:58.44+00:00

I have created a very simple project to illustrate my problem. I fear the resolution is simple but for the life of me I can't see it.

I have but one model:

Public Class SECategory
    Public Property CategoryID As Integer
    Public Property Plural As String
....
End Class

The MainWindow to display the model has only a ComboBox:

<Window>
....
<Window.DataContext>
    <self:ListsVM />
        </Window.DataContext>
    <i:Interaction.Triggers>
        <i:EventTrigger
            EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadMe}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListBox
        DisplayMemberPath="Plural"
        ItemsSource="{Binding Source=SECategories}"
        Margin="20"
        SelectedItem="{Binding Path=CurrentSECategory}" />
</Window>

The ViewModel is (leaving out the boilerplate):

Public Class ListsVM
    Implements INotifyPropertyChanged

    Private ReadOnly Property clsLoadMe As New RelayCommand(AddressOf PerformLoadMe)
    Private Sub PerformLoadMe()
        clsSECategories = <<A populated ObservableCollection(Of SECategory) 
    End Sub

    Public Property CurrentSECategory As SECategory
    Public Property SECategories As ObservableCollection(Of SECategory)

<<Other boilerplace>>
End Class

When I run this, it populates the ClmboBox the items are blank and I get this error:

System.Windows.Data Error: 40 : BindingExpression path error: 'Plural' property not found on 'object' ''Char' (HashCode=6881385)'. BindingExpression:Path=Plural; DataItem='Char' (HashCode=6881385); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

I hope you can spot the mistake.

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,681 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,580 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 40,786 Reputation points Microsoft Vendor
    2023-02-02T06:42:41.7466667+00:00

    Your code is listbox, so I tested the listbox binding. You could refer to the following code.

    <Window x:Class="MainWindow"
           
            xmlns:self="clr-namespace:DataBindindDeml"
            mc:Ignorable="d"
           xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
            Title="MainWindow" Height="450" Width="800">
        <Window.DataContext>
            <self:ListsVM />
        </Window.DataContext>
     
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding LoadMe}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListBox  
            ItemsSource="{Binding SECategories}"
            Margin="20" Width="300" Height="300"
            SelectedItem="{Binding Path=CurrentSECategory}" >
           
        </ListBox>
    </Window>
    

    Codebehind:

    Imports System.Collections.ObjectModel
    Imports System.ComponentModel
    Imports GalaSoft.MvvmLight.Command
    
    Class MainWindow
    
    End Class
    Public Class ListsVM
        Implements INotifyPropertyChanged
    
        Private ReadOnly Property clsLoadMe As New RelayCommand(AddressOf PerformLoadMe)
        Public ReadOnly Property LoadMe As RelayCommand
            Get
                Return clsLoadMe
            End Get
        End Property
    
        Private Sub PerformLoadMe()
            clsSECategories.Add(New SECategory() With {
            .CategoryID = 1,
            .Plural = "user1"
        })
            clsSECategories.Add(New SECategory() With {
            .CategoryID = 2,
            .Plural = "user2"
        })
            clsSECategories.Add(New SECategory() With {
            .CategoryID = 3,
            .Plural = "user3"
        })
            clsSECategories.Add(New SECategory() With {
            .CategoryID = 5,
            .Plural = "user4"
        })
    
        End Sub
        Private clsSECategories As ObservableCollection(Of SECategory) = New ObservableCollection(Of SECategory)()
    
    
        Public Property SECategories As ObservableCollection(Of SECategory)
            Get
                Return clsSECategories
            End Get
            Set(ByVal value As ObservableCollection(Of SECategory))
    
    
                clsSECategories = value
                NotifyPropertyChanged("SECategories")
    
            End Set
        End Property
    
        Public Property CurrentSECategory As SECategory
    
        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
    Public Class SECategory
        Public Property CategoryID As Integer
        Public Property Plural As String
        Public Overrides Function ToString() As String
            Return Plural
        End Function
    End Class
    
    

    The result:

    User's image


    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.