Item Template Not Using Data Context

RogerSchlueter-7899 1,256 Reputation points
2024-07-27T22:55:00.58+00:00

Here is code for WPF window (omitting irrelevant stuff):

XAML

<ListBox
	x:Name="lbxEvents">
	<ListBox.ItemTemplate>
		<DataTemplate>
			<TextBox
				BorderThickness="0"
				FontWeight="SemiBold"
				Padding="0,2,0,2"
				Text="{Binding Path=Title, Mode=OneWay}" />
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>

Code behind:

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

Public Structure evt
	Public Beginning As Date
	Public CategoryID As Integer
	Public Title As String
End Structure

Private Sub PopulateUpcomingEvents()
	Dim ev As evt
	Dim evts As New List(Of evt)
	<<Events are populated correctly>>
	lbxEvents.ItemsSource = evts
End Sub

Error message:

System.Windows.Data Error: 40 : BindingExpression path error: 'Title' property not found on 'object' ''evt' (HashCode=-13885767)'. BindingExpression:Path=Title; DataItem='evt' (HashCode=-13885767); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

I cannot see why this error occurs.

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