wpf - 绑定(bind)到父控件中的属性

标签 wpf xaml

我有以下情况:

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

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

<ListView ItemsSource="{Binding Path=ProductItems}" AlternationCount="{Binding Path=ProductItems.Count}">
...

对于标签,我有以下内容:

<Label x:Name="LabelID" 
       Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, 
       Path=(ListView.AlternationIndex)}"/>

但是它的LabelID只显示0.. 所以我认为 TemplatedParent 没有指向 ListView 控件。 那么我如何更正绑定(bind)以指向“上级父级”,在我的例子中是 ListView ?

提前致谢

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

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

更新: 这是正确的 xaml,绑定(bind)应设置为 RelativeSource=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>

关于wpf - 绑定(bind)到父控件中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15789090/

相关文章:

.net - 在Visual Studio中,从后面的代码切换到Xaml代码的快捷方式是什么?

c# - 在哪里可以找到 WPF 剪贴画?

wpf - 在 DataGrid 的选定行中设置元素的前景色

c# - 设置 Window.Content 不会破坏之前存在的 UI?

VS2008 Express 中的 WPF 稳定性

c# - wpf 组合框默认从 itemssource 选择

c# - 如何让DataGrid整体单站关注方向键行选择遍历?

c# - WPF - 标题边框

c# - 在 UWP 中,有没有一种方法可以让列相互环绕?

c# - 如何处理包含列表的 WPF 用户控件中的嵌套绑定(bind)?