Item Template Not Using Data Context
RogerSchlueter-7899
1,326
Reputation points
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.
Sign in to answer