Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListBox binding when IsSynchronizedWithCurrentItem=true

Tags:

wpf

listbox

<ListBox HorizontalAlignment="Right" 
   ItemsSource="{Binding Groups}" 
   SelectedValue="{Binding SelectedGroup}"/>

On startup, before the screen displayed, my viewmodel had SelectedGroup = Groups.First() (and Groups contained several items).

I spent about an hour trying to figure out why my item was not being selected. When I clicked (or pressed spacebar), then the selected item appeared selected, but before that, the item did not appear selected.

After I added <ListBox IsSynchronizedWithCurrentItem ="True">, the app started working.

Question: Can someone explain why this is necessary? In other words, why would Microsoft have even made this an option? Wouldn't I always want this behavior?

like image 343
agent-j Avatar asked Aug 28 '26 08:08

agent-j


1 Answers

Consider this example: Example

While using a ComboBox, it illustrates the idea: You don't want the first item automatically selected.

like image 98
Mike Veigel Avatar answered Sep 02 '26 15:09

Mike Veigel