我有以下情况:
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>
use*_*519 22
请试试这个:
 <Label Content="{Binding (ListView.AlternationIndex),
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"  />
更新:这里是corret xaml,绑定应该是relativae source = ListViewItem,但网格列宽度有问题:
  <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn x:Name="gvc" Header="id"  Width="30">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding (ListView.AlternationIndex),
                                RelativeSource={RelativeSource FindAncestor,
                                    AncestorType={x:Type ListViewItem}}}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Product name" 
                                DisplayMemberBinding="{Binding Path=ProductName}"  
                                Width="auto" />
            </GridView>
        </ListView.View>
您可以使用
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}} 
找到您上方的特定控件
| 归档时间: | 
 | 
| 查看次数: | 33343 次 | 
| 最近记录: |