c# - WPF 嵌套绑定(bind)令人头疼 : access outer ListView with nested ListView

标签 c# wpf data-binding

我有这个:

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

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

我试过这个:

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

我认为这可行,但它给了我binding can only be set on a dependencyproperty of a dependencyobject 错误。

编辑:发布完整绑定(bind)

最佳答案

这个:

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

错了。您不能在另一个 BindingPath 属性上设置 {Binding}。你需要的是:

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

问题是您似乎混淆了属性语法属性元素语法。您可能想在 MSDN 上阅读有关这些概念的更多信息.

关于c# - WPF 嵌套绑定(bind)令人头疼 : access outer ListView with nested ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24999653/

相关文章:

c# - System.Net.NetworkInformation.Ping 崩溃

wpf - 如何将 WPF xaml 表单的 Design DataContext 设置为使用泛型类型参数的类

.net - 为什么 .Net WPF DependencyProperties 必须是类的静态成员

javascript - polymer 数据绑定(bind)和 Immutable.js 结合在一起

c# - 如果数据库表更新/插入,如何让 Windows C# 控件自动更新?

c# - DataGrid 的自定义列模板

c# - 约会时间?串起来

c# - 将集合绑定(bind)到 Ninject

c# - 匿名类型属性 setter

wpf - WPF中的MediaStreamSource