0
votes

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.

1
why cant you set the DataContext of ItemsControl as SelectedFile?Abin
If there is no other element in the hierarchy between the ScrollViewer and the ItemsControl where a DataContext is set, removing the DataContext assignment from the ScrollViewer and setting ItemSource="{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
@AbinMathew I couldn't get that to work. Working off of a single view model, I would normally just set the DataContext for the window as Clemens suggested. I thought it might be a notification issue, but the view model instance contains blank data before it's set as the DataContext, so it should have shown something. I'll work on posting a full example.J Straight
@Clemens My "slim" version actually works as expected, so I went back to my original project. For the moment, removing Window.DataContext entry in the XAML is making it work. I thought it was only a type reference for helping with auto-complete in bindings, but I guess it's doing more than that. I'm using parameterized constructors in a few places, but making sure everything had a parameterless constructor didn't help.J Straight

1 Answers

0
votes

That happens to me a lot. My guess the problem is when SelectedItem is null, then you get null reference exception that gets eaten by the control.

It should work if you move the DataContext={Binding SelectedItem} from ScrollViewer to the ItemsControl.