Selecting a Textbox Item in a Listbox does not change the selected item of the listbox

后端 未结 15 1397
清酒与你
清酒与你 2020-11-27 04:10

I Have a wpf Listbox that display\'s a list of textboxes. When I click on the Textbox the Listbox selection does not change. I have to click next to the TextBox to select th

15条回答
  •  执笔经年
    2020-11-27 04:58

    Is there some property I need to set for the Textbox to forward the click event to the Listbox?

    It's not a simple property, but you can handle the GotFocus event on your TextBox, then use VisualTreeHelper to find the ListBoxItem and select it:

    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox myTextBox = sender as TextBox;
        DependencyObject parent = VisualTreeHelper.GetParent(myTextBox);
        while (!(parent is ListBoxItem))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }
        ListBoxItem myListBoxItem = parent as ListBoxItem;
        myListBoxItem.IsSelected = true;
    }
    

提交回复
热议问题