Click here to Skip to main content
15,353,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF ListBox with the ItemsSource bound to a ViewModels list of MyObjects (products). This works fine, the lisbox will be filled with products from Database. Unfortunatly there are over 4.000 entries in the DB. When the user navigates to the listbox, a specific product entry should be selected and focused, just like when the user clicks a specific entry with the mouse. Is the a simple way in WPF to solve this?
Binding the selected item with XAML like this works:
XML
<ListBox SelectedItem="{Binding SelectedArtikel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

but the selected item is somewhere deep in the list. The selected item is marked as selected but not in sight, you have to scroll down manually.

Regards

What I have tried:

I tried:
C#
lbxArtikel.UpdateLayout();
var lbxItem = (ListBoxItem)lbxArtikel
	.ItemContainerGenerator
	.ContainerFromItem(lbxArtikel.SelectedItem);
lbxItem.Focus();
But this selected only the first entry.
Posted
Updated 20-May-22 11:29am
Comments
U. G. Leander 30-Mar-16 8:35am
   
You could try lbxArtikel.ScrollIntoView(lbxArtikel.SelectedItem),
assuming lbxArtikel is your ListBox and then set the focus on the item.
susad 30-Mar-16 8:43am
   
Thank You!! That's it!
U. G. Leander 30-Mar-16 10:03am
   
You're welcome! Could you please mark the question as solved, so that it doesn't appear in the "unanswered"-category?

1 solution

C#
lbxArtikel.ScrollIntoView(lbxArtikel.SelectedItem)

thx to U.G.Leander!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900