Skip to content

Selelect ListBoxItem when focus TextBox inside ItemTemplate #11208

Closed Answered by timunie
workgroupengineering asked this question in Q&A
Discussion options

You must be logged in to vote

You could use an EventHandler for TextBox.GotFocus which sets SelectedItem on parent ListBox like this:

! <!-- Do NOT place ListBox inside StackPanel as it will break virtualization -->
<DockPanel LastChildFill="True">
    <TextBlock Text="{Binding Greeting}" 
               DockPanel.Dock="Top"/>
    <ListBox ItemsSource="{Binding  Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Name}"
+                        GotFocus="TextBox_OnGotFocus"
                         Margin="5"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DockPanel>

and in the event handler:

private void TextBox_OnGotFocus(object? s…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@gmccardle
Comment options

Answer selected by workgroupengineering
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants