Mariano
Mariano

Reputation: 3018

How to find a ListView in a DataTemplate?

I have this ListView in xaml

    <ListView x:Name="PersonsListView" ItemsSource="{Binding}" ItemTemplate="{DynamicResource personLayout}">
    <ListView.Resources>
        <DataTemplate x:Key="personLayout" DataType="Person">
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Path=FullName}"/>
                <ListView x:Name="AddressesListView" ItemsSource="{Binding Path=Addresses}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.Resources>
</ListView>

How can I use AddressesListView in code behind? For instance if I want to do AddressesListView.SelecItem.

Upvotes: 0

Views: 1689

Answers (2)

Dominic Hopton
Dominic Hopton

Reputation: 7292

Given an item in the PersonsListView that is of Type Person, you can use the ItemContainerGenerator property on the PersonsListView, and find the container for the data item. You should then bable to use FrameworkElement.FindName(), to find that specific element.

The nested listview looks kinda weird BTW :)

Upvotes: 2

Related Questions