i have the following case:
Window
|_Grid
|_ListView (MainProductGrid)
|_View
|_GridView
|_GridViewColumn
|_CellTemplate
|_DataTemplate
|_Label (LabelID)
now, i want to display in the LabelID the index of the row in the ListView.
so i made the following:
<ListView ItemsSource="{Binding Path=ProductItems}" AlternationCount="{Binding Path=ProductItems.Count}">
...
and for the label i have the following:
<Label x:Name="LabelID"
Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ListView.AlternationIndex)}"/>
but it LabelID is only showing 0..
so i think the TemplatedParent is not pointing to the ListView control..
so how can i correct the binding to point to the "upper parent" which is in my case the ListView ?
thanks in advance
################
Update:
here is the complete 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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…