Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

wpf - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

please try this :

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

Update: here is the corret xaml, the binding should be to relativae source=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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...