wpf 绑定到父控件中的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15789090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:45:31  来源:igfitidea点击:

bind to a property in the parent control

wpfxaml

提问by lebhero

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:

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

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

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

thanks in advance

提前致谢

################

Update: here is the complete xaml ...

更新:这是完整的 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>

回答by user1064519

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:

更新:这里是 corret xaml,绑定应该是 relativae source=ListViewItem,但网格列宽有问题:

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

回答by mlemay

You can use

您可以使用

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

to find a specific control above you

找到您上方的特定控件

回答by DHN

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

好吧,由于您处于 a 的上下文中,因此DataTemplate您无法通过TemplatedParent模式访问该属性,至少在您的情况下是这样。它指的是应用模板(数据绑定元素存在于其中)的元素。[...]链接我不确定它是否可以在 a 中使用,DataTemplate因为我只在 中看到过它ControlTemplates,但是由于文档没有说明其他情况......

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

您可以做的是尝试找到Ancestor. 例如

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

I didn't used in DataTemplatesyet, so no warranty.

我还没用过DataTemplates,所以没有保修。