绑定到父控件中的属性 [英] bind to a property in the parent control

查看:28
本文介绍了绑定到父控件中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

Window
  |_Grid
      |_ListView (MainProductGrid)
           |_View
               |_GridView
                    |_GridViewColumn
                             |_CellTemplate
                                     |_DataTemplate
                                             |_Label (LabelID)

现在,我想在 LabelID 中显示 ListView 中行的索引.所以我做了以下:

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)}"/>

但它的 LabelID 只显示 0..所以我认为 TemplatedParent 没有指向 ListView 控件..那么我如何更正绑定以指向上层父级",在我的情况下是 ListView ?

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 ?

提前致谢

更新:这是完整的 xaml ...

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>

推荐答案

请试试这个:

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

更新:这是corret xaml,绑定应该是relativae source=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>

这篇关于绑定到父控件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆