WPF ListView控件绑定到列标题 [英] WPF LIstView Binding to Column Headers
问题描述
我试图创建一个动态绑定到一组日期的列表视图。因此,用户将b能够选择日期范围和选择的日期,结果会显示与列标题的日期。我有这一切只有一个问题的工作,日期不会在头部出现。我有,我看不出有任何理由为什么它不工作的情况如下:
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 show up with the date in the column header. I've got it all working with just one problem, the dates don't show up in the header. I've got the following which I can't see any reason why it doesn't work:
public class KPIResult : DependencyObject
{
public static readonly DependencyProperty DateProperty = DependencyProperty.Register("Date", typeof(DateTime), typeof(KPIResult), new UIPropertyMetadata(null));
public DateTime Date
{
get { return (DateTime)GetValue(DateProperty); }
set { SetValue(DateProperty, value); }
}
public static readonly DependencyProperty ResultProperty = DependencyProperty.Register("Result", typeof(int), typeof(KPIResult), new UIPropertyMetadata(null));
public int Result
{
get { return (int)GetValue(ResultProperty); }
set { SetValue(ResultProperty, value); }
}
}
而code为ListView:
And the code for the ListView:
<ListView Margin="6" ItemsSource="{Binding ElementName=This, Path=KPICollection}" Name="lvKPIView" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Width="40" >
<GridViewColumnHeader Tag="KPIResult[0]" Content="{Binding Path=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>
结果显示就好了。在头只是没有日期:(
The results are displayed just fine. Just no dates in headers :(.
任何想法家伙?
干杯,
SumGuy
Cheers, SumGuy
推荐答案
试试这个方法:
<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>
显然,问题是,你试图绑定到一个属性不指定绑定源,也不必的DataContext
设置为任何的控制。之所以内部结合 CellTemplate
的工作是,对行的数据上下文被自动设定为相应的列表项实例,但事实却并非如此的标题 - 他们继承的数据上下文父控件。所以,如果我们指定的DataContext
为的ListView
然后绑定在头使用将有一个相对路径数据方面: {结合KPICollection.KPIResults [0] .Date}
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}
这篇关于WPF ListView控件绑定到列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!