我有以下情况:
Window
|_Grid
|_ListView (MainProductGrid)
|_View
|_GridView
|_GridViewColumn
|_CellTemplate
|_DataTemplate
|_Label (LabelID)
现在,我想在 LabelID 中显示 ListView 中行的索引。所以我做了以下事情:
<ListView ItemsSource="{Binding Path=ProductItems}" AlternationCount="{Binding Path=ProductItems.Count}">
...
对于标签,我有以下内容:
<Label x:Name="LabelID"
Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ListView.AlternationIndex)}"/>
但它 LabelID 只显示 0 .. 所以我认为 TemplatedParent 没有指向 ListView 控件.. 那么我怎样才能更正绑定以指向“上父级”,在我的情况下是 ListView ?
提前致谢
################更新:这是完整的 xaml ...
<Grid x:Name="mainGrid">
<ListView ItemsSource="{Binding Path=ProductItems}" AlternationCount="{Binding Path=ProductItems.Count}" x:Name="MainProductGrid">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn x:Name="gvc" Header="id" Width="auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding (ListView.AlternationIndex),RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Product name" DisplayMemberBinding="{Binding Path=ProductName}" Width="auto" />
</GridView>
</ListView.View>
</ListView>
</Grid>