Item Template Not Using Data Context

RogerSchlueter-7899 1,591 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.

Developer technologies | Windows Presentation Foundation
{count} votes

Your answer

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