I have window with the DataContext set to an instance of my view model. My view model has a property 'SelectedFile' of a custom class. That custom class has a property 'RevHistory' of type ObservableCollection where Revision is a custom class with a few string properties. RevHistory is initialized to a fixed size. My window has a part with an ItemsControl as follows (extra content removed)
<Window>
<Grid Grid.IsSharedSizeScope="True">
...
<Border Grid.Column="1" Grid.Row="3" Grid.RowSpan="3" BorderBrush="{x:Static SystemColors.ControlDarkBrush}" BorderThickness="2" Margin="0,0,5,5" CornerRadius="3">
<Grid>
...
<ScrollViewer DataContext="{Binding SelectedFile, Mode=TwoWay}" Grid.Row="1" Margin="5" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Auto" VerticalAlignment="Top">
<Grid>
...
<Border Grid.Row="1" Grid.Column="0" BorderThickness="1,0,0,0" BorderBrush="{x:Static SystemColors.ActiveBorderBrush}">
<ItemsControl ItemsSource="{Binding RevHistory}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox BorderThickness="0,0,0,1" Text="{Binding Rev}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" TextAlignment="Center" Padding="2"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
...
</Grid>
</ScrollViewer>
...
</Grid>
</Border>
</Grid>
What I'm having trouble understanding is why it only works when the DataContext binding is made at the ScrollViewer. Does the ScrollViewer block further DataContext inheritance? With the ItemsControl ItemSource={Binding SelectedFile.RevHistory} no items are shown. Going up the visual tree from the ItemsControl, the ScrollViewer was the first place that binding the DataContext worked.
DataContext
ofItemsControl
asSelectedFile
? – AbinItemSource="{Binding SelectedFile.RevHistory}"
must work. There should not be any explicit DataContext assignment except the one at the top level, i.e. the window. Please try to provide a minimal reproducible example. – Clemens