其他分享
首页 > 其他分享> > WPF嵌套绑定头痛:使用嵌套的ListView访问外部ListView

WPF嵌套绑定头痛:使用嵌套的ListView访问外部ListView

作者:互联网

我有这个:

<ListView ItemsSource="{Binding MyFirstCollection} >
    <ListView.View>
        <GridView>
            <GridViewColumn>
                 <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ListView ItemsSource="{Binding DataContext.MyOtherCollection, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" >
                            <GridView>
                            <GridViewColumn>
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Content="{Binding}">

                                            <!-- the error is in here somewhere -->
                                            <CheckBox.IsChecked>
                                                <MultiBinding Converter="{StaticResource OrderExclusionConverter}">
                                                    <Binding Path="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type ListView}}}" />
                                                    <Binding Path="MemberCount.MemberCountID" />
                                                </MultiBinding>
                                            </CheckBox.IsChecked>

                                        </CheckBox>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>                         </ListView>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

如果我陷入“错误在这里某处”的内部,如何获得绑定以访问MyFirstCollection中的当前项目?

我尝试了这个:

Path="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type ListView}}}"

我以为可以用,但是它给我的绑定只能在依赖对象错误的依赖属性上设置.

编辑:发布完整绑定

解决方法:

这个:

<Binding Path="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type ListView}}}" />

是错的.您不能在另一个绑定的Path属性上设置{Binding}.您需要的是:

<Binding Path="DataContext" 
         RelativeSource="{RelativeSource AncestorType=ListView, AncestorLevel=2}"/>

问题是您似乎在属性语法和属性元素语法之间感到困惑.您可能想在MSDN上阅读有关这些概念的更多信息.

标签:data-binding,wpf,c
来源: https://codeday.me/bug/20191121/2051810.html