How can I access the ListViewItems of a WPF ListView?
        Posted  
        
            by David Schmitt
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by David Schmitt
        
        
        
        Published on 2008-09-18T13:02:56Z
        Indexed on 
            2010/04/26
            2:33 UTC
        
        
        Read the original article
        Hit count: 528
        
Within an event, I'd like to put the focus on a specific TextBox within the ListViewItem's template. The XAML looks like this:
<ListView x:Name="myList" ItemsSource="{Binding SomeList}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <!-- Focus this! -->
                        <TextBox x:Name="myBox"/>
I've tried the following in the code behind:
(myList.FindName("myBox") as TextBox).Focus();
but I seem to have misunderstood the FindName() docs, because it returns null.
Also the ListView.Items doesn't help, because that (of course) contains my bound business objects and no ListViewItems.
Neither does myList.ItemContainerGenerator.ContainerFromItem(item), which also returns null.
© Stack Overflow or respective owner