WPF LIstView Binding to Column Headers

前端 未结 1 681
暖寄归人
暖寄归人 2021-01-16 19:15

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

相关标签:
1条回答
  • 2021-01-16 19:46

    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}

    0 讨论(0)
提交回复
热议问题