Binding not working in <ItemsControl.ItemTemplate>


I can not get the binding of the Text property for a DataTemplate in the MVVM design pattern to work.

My code is as follows:


<Button Grid.Row="0" Content="{Binding ButtonText}" />
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
   <ItemsControl ItemsSource="{Binding MyItems}">
               <Label Content="aaaaa" />
               <TextBlock Text="{Binding ItemText}" />


public MainWindow()
   DataContext = new MainWindowViewModel();


public ObservableCollection<object> MyItems => MyConverter.GetCollection(MyData.List);
public string ItemText => "dddd"; //  this DOES works
public string ItemText => AnObject.Text; // this does NOT work
public string ButtonText => AnObject.Text; // this DOES works (note, same object property!)

Why does my binding inside the DataTemplate not work?

The binding inside the DataTemplate does not work because the ItemText property in MainWindowViewModel.cs is not a property of the item in the MyItems collection. To fix this issue, you need to change the MyItems collection to be of type ObservableCollection<MyItem> (assuming MyItem is the class of the items in the collection) and add the ItemText property to the MyItem class. Then, you can bind to the ItemText property inside the DataTemplate.