17

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>
lebhero
  • 1,391
  • 5
  • 18
  • 35

3 Answers3

32

Please try:

<Label Content="{Binding (ListView.AlternationIndex),
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"  />

Update: Here is the correct xaml, the binding should be set to RelativeSource=ListViewItem, yet there was a problem with the grid column width:

<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>
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
user1064519
  • 2,180
  • 12
  • 13
  • 1
    no, didnt work ...cant see how it would work if iam looking in the ListViewItem instead the ListView itself !?? – lebhero Apr 03 '13 at 14:14
  • edited my answer, worked for me! you should refer to the item because you want each item index – user1064519 Apr 03 '13 at 14:16
  • thanks for your posting..i will test it tomorrow and will let you know if it works ..cheers – lebhero Apr 03 '13 at 20:30
  • {Binding Path=AlternationIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" – Stepagrus Sep 14 '18 at 10:06
5

You can use

RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}} 

to find a specific control above you

mlemay
  • 1,622
  • 2
  • 32
  • 53
2

Well since you're in the context of a DataTemplate you cannot access the property via TemplatedParent mode, at least in your case. It refers to the element to which the template (in which the data-bound element exists) is applied. [...] Link I'm not sure, that it can be used in a DataTemplate because I've only seen it in ControlTemplates, but since the docs are not telling otherwise...

What you can do is to try find the Ancestor. E.g.

<Label Content="{Binding AlternationIndex, 
       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}" ... />

I didn't used in DataTemplates yet, so no warranty.

DHN
  • 4,807
  • 3
  • 31
  • 45