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

后端 未结 15 1416
清酒与你
清酒与你 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 05:04

    I use a class handler to set this behavior. Doing it this way will fix all of the list views in the application. I don't know why this is not the default behavior.

    In your App.xaml.cs, add the following to OnStartup:

    protected override void OnStartup(StartupEventArgs e)
        {
            EventManager.RegisterClassHandler(typeof (ListViewItem), 
                                              ListViewItem.PreviewGotKeyboardFocusEvent,
                                              new RoutedEventHandler((x,_) => (x as ListViewItem).IsSelected = true));
        }
    

提交回复
热议问题