Bob Tway
Bob Tway

Reputation: 9603

WPF Nested binding headache: access outer ListView with nested ListView

I've got this:

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

If I'm down inside "the error is in here somewhere", how do I get a binding to access the current item in MyFirstCollection?

I tried this:

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

Which I thought would work, but it gives me binding can only be set on a dependencyproperty of a dependencyobject errors.

EDIT: posted full binding

Upvotes: 3

Views: 1031

Answers (1)

Fede
Fede

Reputation: 44028

This:

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

is wrong. You can't set a {Binding} on the Path property of another Binding. What you need is this:

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

The problem is you seem to be confused between the Attribute Syntax and the Property Element Syntax. You may want to read more about these concepts on MSDN.

Upvotes: 3

Related Questions