C# - WPF - Access TextBox inside of a listview?

946 views Asked by At

I have a listview with three columns, When I select a listview item, how can I access the TextBox of the selected item?

In my case I would like to focus the TextBox "textQuantity" when I select an item.

        <ListView x:Name="EntryListView" Height="Auto" 
                  ItemsSource="{Binding TheList}"   
                  MouseDoubleClick="EntryListView_MouseDoubleClick"  
                  SelectionChanged="EntryListView_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Quantity">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Name="textQuantity" Text="{Binding DefaultQuantity}" Width="40" IsTabStop="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>                        
                    <GridViewColumn Header="Block1">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Block1}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Block2" Width="Auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Block2}"></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

I Guess I need to do something in Selectionchanged?

    private void EntryListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

UPDATE: Just trying to explain more:
enter image description here

When I select the first row of the listview, by tabing into it, or selecting the row by left click. I want to be able to write into the Quantity-TextBox of the selected ListView item.

For example, If I press on Test1, I want to directly be able to write into the field where the text is 100. Now I have to press inside the TextBox to be able to edit the value.

1

There are 1 answers

0
kidshaw On

You're binding the item source to a collection, create a property of the same type as your collection. Then bond your listbox selecteditem to that property.

Alternatively bind to a selected index property.

Either gives you a way to identify which item in the collection is selected.