Binding Errors in a Simple Project

RogerSchlueter-7899 1,326 Reputation points
2024-04-16T22:49:29.3466667+00:00

I have carved out a simple project from a larger one to demonstrate the binding errors I am getting so the names I use might seem ... odd. The following is the ENTIRE project:

Window x:Class="MainWindow"
		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"
		mc:Ignorable="d"
		Title="MainWindow"
		Height="200"
		Width="100">
	<ListBox
		x:Name="lbxRoutes"
		DisplayMemberPath="Name"
		ItemsSource="{Binding Source=Routes}"
		Margin="10,13,10,0"
		SelectedValue="{Binding RouteID}"
		SelectedValuePath="ElementID"
		SelectionMode="Single">
	</ListBox>
</Window>

The code behind:

Class MainWindow
	Public Sub New()
		InitializeComponent()
		DataContext = Me
	End Sub

	Private Sub LoadMe(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
		Routes = New List(Of GeoElement) From {
			New GeoElement With {.ElementID = 0, .Name = "abc"},
			New GeoElement With {.ElementID = 1, .Name = "def"},
			New GeoElement With {.ElementID = 2, .Name = "ghi"}
		}
	End Sub

	Private clsRoutes As List(Of GeoElement)
	Public Property Routes As List(Of GeoElement)
		Get
			Return clsRoutes
		End Get
		Set(value As List(Of GeoElement))
			clsRoutes = value
		End Set
	End Property

	Public Property ElementID As Integer
	Public Property RouteID As Integer
End Class

and finally the one class:

Public Class GeoElement
	Public Property ElementID As Integer
	Public Property Name As String
End Class

Here are the two errors I am getting:

System.Windows.Data Error: 40 : BindingExpression path error: 'ElementID' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=ElementID; DataItem='Char' (HashCode=7536755); target element is 'ListBox' (Name='lbxRoutes'); target property is 'NoTarget' (type 'Object')

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

I cannot figure out why these errors are happening.

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

Accepted answer
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2024-04-17T03:21:49.4233333+00:00

    Hi,@RogerSchlueter-7899 . Welcome to Microsoft Q&A. You could update the code ItemsSource="{binding source=route}" to ItemsSource="{Bind Route}" and change the code-behind.

     <Grid>
    
        <ListBox
    
    	x:Name="lbxRoutes"
    
    	DisplayMemberPath="Name"
    
    	ItemsSource="{Binding Routes}"
    
    	Margin="10,13,10,0"
    
    	SelectedValue="{Binding RouteID}"
    
    	SelectedValuePath="ElementID"
    
    	SelectionMode="Single">
    
        </ListBox>
    
    </Grid>
    

    Codebehind:

    Class MainWindow
    	Public Sub New()
    		InitializeComponent()
    		DataContext = Me
    	End Sub
    	Private clsRoutes As List(Of GeoElement) = New List(Of GeoElement) From {
    			New GeoElement With {.ElementID = 0, .Name = "abc"},
    			New GeoElement With {.ElementID = 1, .Name = "def"},
    			New GeoElement With {.ElementID = 2, .Name = "ghi"}
    		}
    	Public Property Routes As List(Of GeoElement)
    		Get
    			Return clsRoutes
    		End Get
    		Set(value As List(Of GeoElement))
    			clsRoutes = value
    		End Set
    	End Property
    
    	Private _routeID As Integer
    	Public Property RouteID As Integer
    		Get
    			Return _routeID
    		End Get
    		Set(ByVal value As Integer)
    
    			If value <> _routeID Then
    				_routeID = value
    			End If
    		End Set
    	End Property
    End Class
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.