Search code examples
wpfxaml

bind to a property in the parent control


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>

Solution

  • 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>