Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind to a property in the parent control

Tags:

wpf

xaml

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>
like image 359
lebhero Avatar asked Apr 03 '13 13:04

lebhero


3 Answers

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>
like image 101
user1064519 Avatar answered Nov 18 '22 12:11

user1064519


You can use

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

to find a specific control above you

like image 30
mlemay Avatar answered Nov 18 '22 12:11

mlemay


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.

like image 2
DHN Avatar answered Nov 18 '22 12:11

DHN