I\'m attempting to create a listview that binds dynamically to a set of dates. So the user would b able to select a date range and the results for the chosen dates would sho
Try this way:
<ListView Margin="6" DataContext="{Binding ElementName=This}" ItemsSource="{Binding KPICollection}" Name="lvKPIView" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Width="40" >
<GridViewColumnHeader Tag="KPIResult[0]" Content="{Binding KPICollection.KPIResults[0].Date}" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=KPIResults[0].Result}" />
<TextBox Text="{Binding Path=KPIResults[0].Result}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Apparently, the problem is that you are trying to bind to a property without specifying binding source and without having DataContext
of the control set to anything. The reason why binding inside CellTemplate
work is that the data context for rows is automatically set to the corresponding list item instance, but this is not true for headers - they inherit data context from the parent control. So, if we specify DataContext
for the ListView
then binding that is used in the header will be have a relative path to that data context: {Binding KPICollection.KPIResults[0].Date}