Search code examples
xamlbindingcomboboxrelativesource

XAML: Binding fiasco with a DataTemplate within a ListView within a DataTemplate


Hi I got a dataTemplate for a ViewModel.

Within this DataTemplate I got a ListView bound to a collection of the template and there In I wan't to be able to set a property of the items in the collection.

The available settings of the property is in another collection in the original ViewModel but I'm having a hard time binding to it.

<DataTemplate DataType="{x:Type ViewModel}">

    <!-- Some irrelevant property bindings -->

    <ListView ItemsSource="{Binding Path=TheCollection}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource SOMEAPPROPRIATERELATIVESOURCE}, 
                                           Path=AvailablePropertiesCollection}"  SelectedItem="{Binding Path=TheProperty}" />
                </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>

Is there some good way to get the binding to the item of the top level DataTemplate?


Solution

  • I believe you would want to use:

    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListView}}, 
        Path=DataContext.AvailablePropertiesCollection}"
    

    This binds to the AvailablePropertiesCollection on the ListView's data context.